开发者

How to hide a table row with jQuery?

开发者 https://www.devze.com 2023-04-11 19:48 出处:网络
I am trying to hide a table row with jQuery instead of with js as in this question. This is the script that I put in the header:

I am trying to hide a table row with jQuery instead of with js as in this question. This is the script that I put in the header:

self.response.out.write("""
    <html>
    <head>
    <link type="text/css" rel="stylesheet" href="/stylesheets/main.css" /> 
    <title>User Admin Page</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
    <script>
    $(document).ready(function() {
        $("#false").click(function() {
        $("#hide").hide("slow");
    });
});
</script>
<body>
""")

And here is the html:

...
<tr class="hide">
 <td>
  ...
   <a class="false" href="/useradminpage?main_id=%s&display=false"><span class="small">(hide)</span></a>
  </td>
 </tr>
</div>
...

This is not working. And this question is same as this but I cannot make mine work. What am I doing wrong?

Update

Edited code according to answers. But this is still not working although it is working in jsfiddle:

         <html><head>
         <link type="text/css" rel="stylesheet" href="/stylesheets/main.css" /> 
         <title>User Admin Page</title>
         <script  src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">

<script>


 $(document).ready(function() {

     $("a.false").click(function(e) {
     $(".hide").hide("slow");
     e.preventDefault();
          });

 });   </script> </head>
         <body>
 ...

Update

The closing </script> was missing in the CDN call; but now the entire table is hidden; I am adding that section of the table. Thanks again for the answers:

    self.response.out.write("""<table class="mytable">
    <tr class="head">
    <th  width="80%">links</th><th>edit tags</th>
    </tr>    
    """)        

    query = Main.all()
    query.filter("owner", user)
    query.filter("display", True)
    query.order("-date")
    cursor = self.request.get("cursor")
    if cursor: query.with_cursor(cursor)
    e = query.fetch(100)
    cursor = query.cursor()

    for item in e:
        main_id = item.key().id()
        self.response.out.write("""
        <tr class="hide">
        <td><a href="%s" target="_blank">%s</a><span class=small> (%s) </span><br />
        <span class=small>%s</span>
        <a href="/edit?main_id=%s"><span class="small">(edit)</span></a>
        <a class="false" href="/useradminpage?main_id=%s&display=false"><span class="small">(hide)</span></a>
        <a href="/comment?main_id=%s"><span class="small">(comments)</span></a></td>
        <td><a href="/tc?url=%s&main_id=%s&user_tag_list=%s" title="edit tags">%s</a>
        </td>
        </t开发者_如何学JAVAr>
        """ % tuple([item.url, item.title, urlparse(item.url).netloc,
        f1.truncate_at_space(item.pitch), main_id, main_id, main_id,
        item.url, main_id, (", ".join(item.tag_list)),
        (", ".join(item.tag_list)),]))


    self.response.out.write("""</tbody></table>""")    


First thing, you have false as a class in your HTML

<a class="false" href=...

and an ID in your script

$("#false").click(function()...

Your hide is also an id and should be class.

Here is the fix: http://jsfiddle.net/A6jKm/1/


EDIT

now it hides the entire table

That's because all your rows are generated with the same hide class, as seen here

for item in e:
    main_id = item.key().id()
    self.response.out.write("""
    <tr class="hide">

To get around this, I've modified the code a bit to search for the direct parent of the clicked item:

$("a.false").click(function(e){
    $(this).parents('tr').hide();
    e.preventDefault();
});

Updated Example: http://jsfiddle.net/A6jKm/3/


EDIT 2

Perhaps closest would work better.

Try this

$("a.false").click(function(e){
    $(this).closest('tr.hide').hide();
    e.preventDefault();
});

Example 3: http://jsfiddle.net/A6jKm/4/


You're using ID selectors:

$("#false").click(function() {
    $("#hide").hide("slow");
});

when you want a class selectors:

$(".false").click(function() {
    $(".hide").hide("slow");
});

Demo: http://jsfiddle.net/ambiguous/sw7Tr/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号