简体   繁体   English

jQuery 遍历

[英]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.我正在克隆相同的 TR onclick 链接按钮,当前链接按钮将被隐藏。 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.您不能/不应该克隆具有 ID 的元素,因为 ID 在文档中应该是唯一的。 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.尽管浏览器很乐意让您拥有两次相同的 ID,但 Javascript 和 jQuery 不会为此原谅您,并且事情不会按预期进行。 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.您知道,ID 必须是唯一的 .. 所以我建议将所有 ID 更改为类。

i think the syntax for parrent should :我认为 parrent 的语法应该是:

$('#master:parent');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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