3

我正在尝试div根据其高度选择 a,如本教程 jQuery Selection所示。我无法让它工作:jsbin 示例。行不通是:

   $('div[height=50px]').css('background-color', 'orange');
4

2 回答 2

1

这是一个等于选择器的属性,所以它匹配的 HTML 是:

<div height="50px"></div>

您可以将其更改为:

$('div[style="height:50px"]').css('background-color', 'orange');

根据评论,上述内容在 Internet Explorer 中不起作用。请尝试以下操作:

$('div').filter(function() {
    return $(this).css('height') == '50px';
}).css('background-color', 'orange');

如果要匹配其他未使用属性指定的高度为 50px 的元素,请查看该.filter()函数。

于 2012-07-31T13:21:37.667 回答
0

如果您的 HTML 是...

<div style="height:50px;"></div>

那么你的选择器应该是......

$('div[style*="height:50px"]');

如果您将高度设置为“50px”和“50px”(常见)的值,则需要相应调整...

$('div[style*="height:50px"], div[style*="height: 50px"]');

如果你改变这个元素的 CSS,div 的 style 属性可能会变成:“height:50px;background-color:orange”,并且不会被这个选择器拾取。

文档:https ://api.jquery.com/attribute-contains-selector/

于 2017-09-19T20:37:16.563 回答