JQuery选择td中的一个元素 [英] JQuery select an element inside a td

查看:298
本文介绍了JQuery选择td中的一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的表的td中选择一个元素,但我真的不理解语法。
这是我试过的:

  $(table> td:#box)

这是我的表结构示例:

 < div id =main> 
< div id =today>
< table id =listwidth =100%cellpadding =4cellspacing =0border =0style =font-size:10px; border-collapse:collapse;> ;
< tr id =109008>
< td class =tdstd>
< a class =boxhref =link>< / a>
< / td>

或使用Chrome的DOM检查器:



解决方案

很好,#box是指具有ID框的DOM对象,因为它是一个唯一的ID。您可以直接选择那个。但是你的代码建议你有几个元素的id框,你必须更改。你应该在TD中为你的元素分配一个类,或者如果它是唯一的DIV或者SPAN,你可以这样访问它:

  $(table td .box)

>选择器意味着TD必须是TABLE的直接子对象,并且我假设您之间至少有一个TR级别,因此也不会工作。我上面的例子匹配每个元素在任何TD内的任何TD的类框是任何TABLE的子。



显然,我将在表上设置一个类,并使用这样的:

  $(table.boxes td .box)



因此,您不会意外地选择您想要使用的范围之外的内容。






您现在已添加HTML,因此我正在编辑我的答案:

  $(table#list a.box)


I would like to select an element inside the td of one of my tables but I don't really understand the syntax. This is what I tried:

$("table > td:#box")

This is a sample of my table structure:

<div id="main">
<div id="today">
    <table id="list" width="100%" cellpadding="4" cellspacing="0" border="0" style="font-size: 10px; border-collapse:collapse;">
    <tr id="109008">
    <td class="tdstd">
    <a class="box" href="link"></a>
    </td>

Or With Chrome's DOM inspector:

解决方案

Well, "#box" means a DOM object with the id "box", as in it being a unique ID. You can select that one directly. But your code suggest that you have several elements with the id "box" which you have to change. You should assign a class to your element inside the TD, or if it's unique by being the only DIV or SPAN in the box, you can access it like this:

$("table td .box")

Note that the ">" selector means that TD has to be a direct child of TABLE, and I'm assuming you have at least a TR level in between, so that won't work either. My example above matches every element with the class "box" inside any TD that is a child to any TABLE.

Obviously I would set a class on the table as well, and use something like this:

$("table.boxes td .box")

Just so you don't accidentally selects things outside the scope you want to work in.


You have now added HTML so I'm editing my answer:

$("table#list a.box")

这篇关于JQuery选择td中的一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆