简体   繁体   中英

How do I target attribute of opacity in jquery?

I know about the attributeContains selector, but how does it apply to style attributes?

I want to find all <a> tags that have their opacity set to 0.

I tried this :

$("a[style*='opacity: 0']")

But it returns nothing.

The :visible selector will not work because it doesn't give consideration to opacity.

To target just those with a 0 opacity, you could use a .filter() to check the .css() value of the opacity:

$("a").filter( function() {
    return $(this).css('opacity') === '0';
});

You could create your own selector if you'd like:

$.extend($.expr[':'], {
    opacity: function(elem, i, attr){
      return( $(elem).css("opacity") === attr[3] + '' );
    }
});

var $invisible = $("a:opacity(0)");

or

$.extend($.expr[':'], {
    transparent: function(elem, i, attr){
      return( $(elem).css("opacity") === "0" );
    }
});

var $invisible = $("a:transparent");

如果您想知道元素是否可见,请使用:

$('a:not(:visible)');

$('a:not(:visible)')

Your code won't work as it only works when the opacity is applied on the element's style attribute - what about CSS styles??? they won't apply. jQuery provides the :visible and :not selectors, so you can combine them. http://api.jquery.com/category/selectors/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM