有没有办法检查元素父级并找到第一个具有CSS背景设置然后返回该背景值的方法?
就像是:
var background = $('element').parents().has(css('background'));
Run Code Online (Sandbox Code Playgroud)
更新:这是我现在使用的代码:
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);
};
Run Code Online (Sandbox Code Playgroud) jquery ×1