有没有办法检查元素的父母,并找到第一个具有CSS背景设置,然后返回该背景值?jQuery:检查元素是否具有CSS属性
喜欢的东西:
var background = $('element').parents().has(css('background'));
UPDATE: 这是我现在使用的代码:
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 – 2010-02-10 19:17:51
函数(x)中'x'究竟做了什么?是一个占位符,还是它实际上被用于某种东西? – adamyonk 2010-02-10 19:29:29
试试这个:if('')alert(“The empty string is true!”); - 你也可以使用!!将布尔值转换为“truthy”值。 – Pointy 2010-02-10 19:52:29