我希望单击具有属性data-capture='noiseClicked'的页面上的所有按钮
到目前为止,这是我的代码:
javascript: (function() {
var followButtons = $("li.js-profile-card button[data-capture='noiseClicked']");
var index = followButtons.length - 1;
follow();
function follow() {
if (index >= 0) {
$(followButtons[index--]).click();
setTimeout(follow, 1);
}
}
})();但是,我希望排除具有li.noise--active或li.friend--active父级的按钮。
因此,将单击以下命令:
<li class="js-profile-card noise--active"><button data-capture="noiseClicked" type="button"></button></li>但以下内容不会被点击..。
<li class="js-profile-card noise--active"><button data-capture="noiseClicked" type="button"></button></li>或
<li class="js-profile-card friend--active"><button data-capture="noiseClicked" type="button"></button></li>我认为jquery的非选择器在这里会有帮助,但我不知道如何使用它来排除具有特定属性的父元素,我也不知道如何排除两个不同的属性(noise--active和friend--active)
谢谢。
发布于 2014-09-29 09:02:10
为此,您可以使用parent & hasClass方法:
var indexToSet = index--;
if( !$(followButtons[indexToSet]).parent().hasClass( 'noise--active' ) && !$(followButtons[indexToSet]).parent().hasClass( 'friend--active' )) {
$(followButtons[indexToSet]).click();
}编辑:
要在节点列表中向上移动,最好使用closest()方法:
var indexToSet = index--;
if( !$(followButtons[indexToSet]).closest( 'noise--active' ).length && !$(followButtons[indexToSet]).closest( 'friend--active' ).length ) {
$(followButtons[indexToSet]).click();
}发布于 2014-09-29 09:12:27
:not选择器可能会派上用场:
var followButtons = $("li.js-profile-card:not(.noise--active,.friend--active) button[data-capture='noiseClicked']");https://stackoverflow.com/questions/26096050
复制相似问题