是否可以将类选择器和属性选择器与jQuery结合使用?
例如,给定以下HTML:
<TABLE>
<TR class="myclass" reference="12345"><TD>Row 1</TD></TR>
<TR class="otherclass" reference="12345"><TD>Row 2</TD></TR>
<TR class="myclass" reference="12345"><TD>Row 3</TD></TR>
<TR class="myclass" reference="54321"><TD>Row 4</TD></TR>
</TABLE>
我只能用来选择第1行和第3行的选择器是什么?
我努力了:
$(".myclass [reference=12345]") // returns nothing
$(".myclass, [reference=12345]") // returns all 4 rows (yes, I know the comma means 'or')
我敢肯定答案很简单,我已经尝试搜索jQuery论坛和文档,但我似乎无法弄清楚。有人可以帮忙吗?
合并它们。从字面上将它们结合起来;将它们连接在一起,没有任何标点符号。
$('.myclass[reference="12345"]')
您的第一个选择器将查找具有属性值的元素,该元素包含在类的元素中。
该空间被解释为后代选择器。
Your second selector, like you said, looks for elements with either the attribute value, or the class, or both.
The comma is being interpreted as the multiple selector operator — whatever that means (CSS selectors don't have a notion of "operators"; the comma is probably more accurately known as a delimiter).
I think you just need to remove the space. i.e.
$(".myclass[reference=12345]").css('border', '#000 solid 1px');
这里有一个小提琴http://jsfiddle.net/xXEHY/
此代码也适用:
$("input[reference=12345].myclass").css('border', '#000 solid 1px');
这也将起作用:
$(".myclass[reference='12345']").css('border', '#000 solid 1px');
本文地址:http://javascript.askforanswer.com/jiangleixuanzeqiheshuxingxuanzeqiyujqueryjiehe.html
文章标签:javascript , jquery , jquery-selectors
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
文章标签:javascript , jquery , jquery-selectors
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!