简体   繁体   中英

jQuery Traversing

My page has this design.

<tr id="master">
    <td id="row1"> <textbot> </td>
    <td id="row2"> <linkbutton> </td>
</tr>

I am cloning the same TR onclick of the link button and the current link button will be hidden. so a new row will be created.

consider I have cloned thrice

<tr id="master">
    <td id="row1"> <textbot> </td>
    <td id="row2"> <linkbutton> </td>
</tr>
<tr id="master">
    <td id="row1"> <textbot> </td>
    <td id="row2"> <linkbutton> </td>
</tr>
<tr id="master">
    <td id="row1"> <textbot> </td>
    <td id="row2"> <linkbutton> </td>
</tr>

Now I want to show the second row's link button.

I am trying with

$(this).parents('#master :last').prev().children('#row2).show();

But its not working.

You can't/shouldn't clone elements that have IDs, because IDs are supposed to be unique in a document. Although a browser will happily let you have the same ID twice, Javascript and jQuery do not forgive you for this and things will not work as expected. The proper way to group elements is by classes.

So if you switch your code to this:

<tr class="master">
  <td class="row1"> <textbot> </td>
  <td class="row2"> <linkbutton> </td>
</tr>

Your selector might look like this:

$('tr.master').eq(1).find('td.row2').show();

you know, ID must be unique .. so i recommend to change all id to class.

i think the syntax for parrent should :

$('#master:parent');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM