【发布时间】:2012-05-25 08:33:44
【问题描述】:
我正在尝试选择所有具有非空data-go-to 属性的元素。
我已经尝试过$('[data-go-to!=""]'),但奇怪的是,如果我这样做,它似乎会选择页面上的每一个元素。
【问题讨论】:
标签: javascript jquery jquery-selectors custom-data-attribute
我正在尝试选择所有具有非空data-go-to 属性的元素。
我已经尝试过$('[data-go-to!=""]'),但奇怪的是,如果我这样做,它似乎会选择页面上的每一个元素。
【问题讨论】:
标签: javascript jquery jquery-selectors custom-data-attribute
作为进一步的参考,最新的 (may'14) (2015 年 8 月) (2016 年 9 月) (17 年 4 月) (18 年 3 月) (3 月'19) (20 年 5 月) (jan'22)...
适用的答案:
###空字符串: 如果
attr必须存在并且可以有任何值 (或根本没有)
jQuery("[href]");
###缺少属性: 如果
attr可以存在并且如果存在,必须有一些价值
jQuery("[href!='']");
###或两者兼有: 如果
attr必须存在并且必须有一些价值......
jQuery("[href!=''][href]");
PS:更多组合是可能的...
###在jsFiddle 中查看此测试示例:
jQuery v1.9.1 ->jsFiddle online test
jQuery v1.11.0 ->jsFiddle online test
jQuery v2.1.0 ->jsFiddle online test
jQuery v2.1.3 ->jsFiddle online test
jQuery v2.2.4 ->jsFiddle online test
jQuery v3.2.1 ->jsFiddle online test
jQuery v3.3.1 ->jsFiddle online test
jQuery v3.4.1 -> jsFiddle online test 1 月 10 日 22 日在 jsFiddle 中提供的最后一个 jQuery 版本
jQuery Edge ->jsFiddle online test jQuery edge version(谨慎使用)
jQuery v3.0.0-alpha1 ->jsFiddle online test
jQuery v3.1.1 Slim ->jsFiddle online test
jQuery v3.2.1 Slim ->jsFiddle online test
jQuery v3.3.1 Slim ->jsFiddle online test
jQuery v3.4.1 Slim -> jsFiddle online test 1 月 10 日 22 日在 jsFiddle 中提供的最后一个 jQuery Slim 版本
###Or here in SO with this Code Snippet.
* Snippet 正在运行 jQuery v2.1.1
jQuery('div.test_1 > a[href]').addClass('match');
jQuery('div.test_2 > a[href!=""]').addClass('match');
jQuery('div.test_3 > a[href!=""][href]').addClass('match');
div,a {
display: block;
color: #333;
margin: 5px;
padding: 5px;
border: 1px solid #333;
}
h4 {
margin: 0;
}
a {
width: 200px;
background: #ccc;
border-radius: 2px;
text-decoration: none;
}
a.match {
background-color: #16BB2A;
position: relative;
}
a.match:after {
content: 'Match!';
position: absolute;
left: 105%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test_1">
<h4>Test 1: jQuery('a[href]')</h4>
<a href="test">href: test</a>
<a href="#">href: #</a>
<a href="">href: empty</a>
<a>href: not present</a>
</div>
<div class="test_2">
<h4>Test 2: jQuery('a[href!=""]')</h4>
<a href="test">href: test</a>
<a href="#">href: #</a>
<a href="">href: empty</a>
<a>href: not present</a>
</div>
<div class="test_3">
<h4>Test 3: jQuery('a[href!=""][href]')</h4>
<a href="test">href: test</a>
<a href="#">href: #</a>
<a href="">href: empty</a>
<a>href: not present</a>
</div>
【讨论】:
$(':not([data-go-to=""])') 不再工作了
试试
$(':not([data-go-to=""])')
更新:
为了不让任何人误入歧途,这个答案将适用于旧版本的 jQuery,但不是面向未来的。由于@gmo 和@siva 的答案似乎都适用于更高版本,因此我尊重(并鼓励您投票)他们的答案....当然希望您度过美好的一天。
【讨论】:
$('element:not([attribute=])'); // gets all of <element attribute=""> 或$(':not([attribute=])'); // gets all of <* attribute="">
$('[data-go-to!=""]:[data-go-to]') 可以。
$('[data-go-to!=""]:[data-go-to]').each(function() {
// Do Your Stuff
});
【讨论】:
Unrecognized Expression 错误。
有'data-attributename'并且它的值不为空:
$('[data-attributename]:not([data-attributename=""])')
'data-attributename' 是否为空:
$('[data-attributename]')
【讨论】:
我不确定一个简单的选择器,但你可以使用filter():
$('[data-go-to]').filter(
function(){
return ($(this).attr('data-go-to').length > 0);
});
参考资料:
【讨论】:
【讨论】:
$('[data-go-to]:not([data-go-to=""])')
【讨论】:
$('[data-go-to]').filter(function() {
return $(this).data('go-to')!="";
});
使用:not、.not()、:empty等只会检查元素本身是否为空,而不是数据属性。为此,您必须根据数据属性值进行过滤。
【讨论】:
这对我有用
$('[attributename]').filter('[attributename!=""]')
【讨论】:
试试这个:
$('[data-go-to:not(:empty)]')
【讨论】:
"Syntax error, unrecognized expression: [data-go-to:not(:empty)]"
这对我有用:
$('.data .title td[data-field!=""]')
【讨论】: