我有两个div元素如下:
<div class="sc-info" tags="abc mno">
....
</div>
<div class="sc-info" tags="abc xyz">
....
</div>
Run Code Online (Sandbox Code Playgroud)
我想根据tags中存在的值执行hide()和show().如下所示
$('div.sc-info[tags with "abc"]').hide() // This should hide both divs
$('div.sc-info[tags with "xyz"]').show() //This should show only second one
Run Code Online (Sandbox Code Playgroud)
使用Attribute Contains Word Selector(~=)选择器
http://api.jquery.com/attribute-contains-word-selector/
$('div.sc-info[tags~="abc"]').hide()
$('div.sc-info[tags~="xyz"]').show()
Run Code Online (Sandbox Code Playgroud)
回复评论:
当我检查多个单词(即)str ="New York"时不工作; [str~ ="abc New York"]没有给出正确的结果.还有其他办法吗?
要在这些属性中选择多个单词,您必须div.sc.info为两个可能的属性选择器进行过滤.
$('div.sc-info').filter('[tags~="abc"],[tags~="New York"]').hide()
Run Code Online (Sandbox Code Playgroud)
示例:http://jsfiddle.net/hunter/AS6Zp/