使用 jQuery 导航到特定元素

发布于 2024-08-23 15:12:26 字数 698 浏览 4 评论 0原文

我有一个表,试图弄清楚如何导航到 3.35 并添加列。有没有办法指定我想用 jQuery 导航到哪个 TD?

提前致谢!

<table>
 <tbody>
   <tr>
    <td>
       <table>
          <tbody>
             <tr>
               <td><a href="">title</a></td>
               <td><a href="">rating</a></td>
               <td>language</td>
               <td>qty</td>
               <td>3.35</td>
               <td><a href="">add</a></td>
             </tr>
          </tbody>
      </table>       
   </td>
  </tr>
 </tbody>
</table>

I have a table, trying to figure out how to navigate to the 3.35 and add columns. Is there a way to specify which TD i want to navigate to with jQuery?

Thanks ahead of time!

<table>
 <tbody>
   <tr>
    <td>
       <table>
          <tbody>
             <tr>
               <td><a href="">title</a></td>
               <td><a href="">rating</a></td>
               <td>language</td>
               <td>qty</td>
               <td>3.35</td>
               <td><a href="">add</a></td>
             </tr>
          </tbody>
      </table>       
   </td>
  </tr>
 </tbody>
</table>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冧九 2024-08-30 15:12:26

尝试使用 :nth-child() 选择器:

$("tr > td:nth-child(5)")

Try the :nth-child() selector:

$("tr > td:nth-child(5)")
疧_╮線 2024-08-30 15:12:26

深入挖掘: http://api.jquery.com/category/selectors/ 和 < a href="http://api.jquery.com/category/traversing/" rel="nofollow noreferrer">http://api.jquery.com/category/traversing/

有几种不同的方法到那里。无论人们在这里提出什么建议,你都应该去了解像这样的基本事情的源头,这样你才能真正理解它,以便将来使用。

Dig in: http://api.jquery.com/category/selectors/ and http://api.jquery.com/category/traversing/

There are several different ways to get there. Whatever people will suggest here, you owe it to yourself to go to the source on a basic thing like this so you really understand it for the future.

憧憬巴黎街头的黎明 2024-08-30 15:12:26

虽然您绝对可以使用 nth-child 选择器 来选择适当的元素,但您最好实际指定一个类来指示列中数据的类型。

原因是示例中的数据具有明显的语义含义,最好在 html 结构中表示它。

然后,从那里,您将根据类进行选择,然后使用位置选择器来确定您正在处理的行。在这种情况下,最好有一个唯一的行 id 存储在 id 属性中,这使得选择更加容易,因为您可以在 jQuery 中使用 id 选择器。

这也使您的 HTML 具有更多语义意义,因为您的行指示架构的实例,这由您分配给表元素的类隐含。

While you could definitely use the nth-child selector to select the appropriate element, you would be better off actually specifying a class which indicates the type of data in the column.

The reason for this is that the data in your example has an obvious semantic meaning, and it's a good idea to represent that in your html structure.

Then, from there, you would select based on the class, and then use a positional selector in order to determine the row you are working on. In that which case, it would be good to have a unique row id that is stored in the id attribute, which makes selection even easier, since you could use the id selector in jQuery.

This plays into your HTML making more semantic sense as well, since your rows are indiciative of instances of your schema, which is implied by the classes that you are assigning to the elements of your table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文