jQuery: check if element has CSS attribute

纵然是瞬间 提交于 2019-12-17 10:26:19

问题


Is there a way to check an elements parents and find the first one that has a CSS background set and then return that background value?

Something like:

var background = $('element').parents().has(css('background'));

UPDATE: This is the code I'm now using:

jQuery.fn.getBg = function(){
    var newBackground = this.parents().filter(function() {
        return $(this).css('background-color').length > 0;
    }).eq(0).css('background-color');
    $(this).css('background-color',newBackground); console.log("new background is: "+newBackground);
};

回答1:


If it's not set, fetching it will yield an empty string. Hence  

var bg = ('element').parents().filter(function() {
    return $(this).css('background').length > 0;
}).eq(0)

EDIT

Some research shows that css('background') will always yield an empty string, or undefined, depending on browser. css('background-color') will correctly return the color of the element at hand; but also different values for each browser, so it is troublesome to test (transparent in IE, rgba(0,0,0,0) in firefox/chrome, for instance, both being accurate ways of specifying transparent).

jQuery.fn.getBg = function() {
    return $(this).parents().filter(function() {
        // only checking for IE and Firefox/Chrome. add values as cross-browser compatibility is required
        var color = $(this).css('background-color');
        return color != 'transparent' && color != 'rgba(0, 0, 0, 0)';
    }).eq(0).css('background-color');
};



回答2:


I believe the correct way to do this is to check the .length property.

In your case you'd want to loop through all of your elements and check for the first one that meets

.css('background').length != 0



回答3:


$('selector').parents().filter(function() { return !!$(this).css('background'); });



回答4:


I'd try something like this:

var background = 0;
$('element').parents().each(function() {
  if (!background && ((e = $(this).css('background')).length > 0)) background = e;
});

Not sure if this exact if-clause works, but the each() should do the trick. The first match will fill the background variable and ignore the rest of the parents while traversing up in the chain.

Edit: Amended to resolve css('background') correctly which may emit an empty string. Additionally, each() does not pass the element, but index and element, thus make use of this instead and discard all parameters.




回答5:


Retrieve element background color or gradient

I came across this as I was trying to apply a background to elements that didn't have one (background gradient or color) already.

This function returns the value of the background gradient or background color if it has one and returns false if it doesn't:

jQuery.fn.getBg = function() {
  var color = $(this).css('background-color');
  var image = $(this).css('background-image');

  if (color != 'transparent' && color != 'rgba(0, 0, 0, 0)') {
    return color;
  }
  if (image != 'none') {
    return image;
  }
  return false;
};

Now if you simply want to test if an element has a background, you can do this using the above function:

var curBack = $(this).getBg();
if (curBack) { /* Element has a background */  }
else { /* Element doesn't have a background */ }

Hopefully this is somewhat resourceful for someone working with gradients.




回答6:


You could do something like:

$('#test').parents().has(css('background')).filter(':first');

Hope it helps : )



来源:https://stackoverflow.com/questions/2239567/jquery-check-if-element-has-css-attribute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!