JQuery 选择器多类对象
大家好,我一直在尝试让一系列对象出现,这些对象具有与它们关联的多个类。
<div class="Foo Bar">
Content
</div>
<script>
alert($('.Foo').length);
</script>
上面的选择器返回 0。不幸的是,这是完全不可能向 google 提出的问题之一(至少到目前为止)据我所知)。
我怎样才能使这个查询工作?
更具体地说,我有一个如下所示的表格:
<table>
<tr class="Location2">
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
</tr>
<tr class="Location3 Territory4">
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
</tr>
</table>
当我编写脚本时:
alert($('.' + (Var that = 'Territory4')).length));
我得到 0。
我精通 HTML 和 CSS,我知道我不应该使用表格等,但这是一个特殊情况。
Hey all, I've been trying to get a series of objects to appear that have multiple classes associated with them
<div class="Foo Bar">
Content
</div>
<script>
alert($('.Foo').length);
</script>
The selector above is returning 0. This is unfortunately one of those questions that is completely impossible to ask to google (at least as far as I can tell).
How can I make this query work?
To be more specific, I have a table that looks like this:
<table>
<tr class="Location2">
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
</tr>
<tr class="Location3 Territory4">
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
</tr>
</table>
When I script:
alert($('.' + (Var that = 'Territory4')).length));
I get 0.
I'm well versed in HTML and CSS and I know I shouldn't use tables etc etc, but this is a special case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:基于更新的问题。
您的代码引发语法错误。将变量赋值从选择器中取出。
选择器是正确的。我猜你的代码是在 DOM 完全加载之前运行的。
试试这个: http://jsfiddle.net/QWNPc/
做:
是相当于doing:
确保在代码运行之前DOM已经加载。
http://api.jquery.com/ready/
EDIT: Based on updated question.
Your code throws a syntax error. Bring the variable assignment out of the selector.
The selector is correct. I'm guessing that your code is running before the DOM is fully loaded.
Try this: http://jsfiddle.net/QWNPc/
Doing:
is equivalent to doing:
which ensures that the DOM has loaded before the code runs.
http://api.jquery.com/ready/