jQuery JQuery选择td内的一个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1676924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery select an element inside a td
提问by Roch
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:
我想在我的一个表的 td 中选择一个元素,但我不太了解语法。这是我尝试过的:
$("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:
或者使用 Chrome 的 DOM 检查器:
回答by Sandman
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:
嗯,“#box”表示一个带有 id 为“box”的 DOM 对象,因为它是一个唯一的 ID。你可以直接选择那个。但是您的代码建议您有几个元素的 id 为“box”,您必须更改这些元素。您应该为 TD 内的元素分配一个类,或者如果它是框中唯一的 DIV 或 SPAN,则它是唯一的,您可以像这样访问它:
$("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.
请注意,“>”选择器意味着 TD 必须是 TABLE 的直接子级,并且我假设您之间至少有一个 TR 级别,因此这也不起作用。我上面的示例将任何 TD 中的每个元素与类“box”相匹配,该类是任何 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:
您现在已经添加了 HTML,所以我正在编辑我的答案:
$("table#list a.box")
回答by Bjarki Heiear
The most efficient selector would be:
最有效的选择器是:
$('#list').find('a.box');
or:
或者:
$('a.box', $('#list')[0]);
By selecting the table id
first you have set your scope to just the table and then you can search for the element that you need with in that table.
通过id
首先选择表格,您已经将范围设置为表格,然后您可以在该表格中搜索您需要的元素。
The second selector is just the same, you are selecting something and you are giving the scope as a second parameter.
第二个选择器是一样的,你正在选择一些东西,你将范围作为第二个参数。
It's just easier to read the first one.
阅读第一个更容易。
回答by Vincent Ramdhanie
$("table tr td .box")
Should do the trick.
应该做的伎俩。
回答by Josh Stodola
This selector...
这个选择器...
table td a.box
tells jQuery to find the a
tag with a class
attribute that contains "box". And this a
tag must be inside a td
that is inside a table
.
告诉 jQuery 查找a
具有class
包含“box”的属性的标签。并且这个a
标签必须在 atd
里面, a里面是 a table
。
回答by cupakob
I'm not sure, but i think, you need $("td#box")
...
我不确定,但我认为,你需要$("td#box")
...