Jquery - 选择所有带有display:block的锚标签

max*_*axp 1 jquery jquery-selectors

我无法理解如何做到这一点?

给定一个充满锚标签的页面,我只想选择具有CSS属性的那些元素display:block.

我想我可以使用jQuery循环(警告伪代码!)

var myarray;
$('a').each(function(arg1, arg2) { 
    if ($(arg2).css('display')=='block')
    myarray.push(arg2);
}
Run Code Online (Sandbox Code Playgroud)

但是,有没有更简单的方法?

Jos*_*kle 9

您可以使用filter函数作为参数.

$('a').filter(function (index) {
                  return $(this).css("display") === "block";
              })
Run Code Online (Sandbox Code Playgroud)


jes*_*sal 6

另一种方法是使用jQuery的属性选择器:

$("a[style$='display: block;']")
Run Code Online (Sandbox Code Playgroud)