0

我想选择所有具有样式属性的 div 内的 html .. 这是小提琴 url Jsfiddle Link

 var testst = $('div').css('margin');
 alert(testst.html());

我无法得到结果。

4

1 回答 1

13

使用属性选择器:

$('div[style]')

但是,使用它来查找具有特定样式(CSS 规则)的元素是个坏主意。它只适用于实际设置的内联样式 usingstyle="whatever"并且您在 selector( div[style="whatever"]) 中的属性值必须与该属性完全匹配。

所以......正确的方法是为class相关元素添加一个属性并使用此类(div.whateverforclass="whatever"class="foo whatever bar")选择它们。

如果您仍然需要查找具有特定 CSS 规则的元素,最可靠的方法是遍历所有元素并检查它们是否具有给定的样式:

$('div').filter(function() {
    return $(this).css('whatever') == 'the_value';
});
于 2013-08-31T11:30:16.450 回答