Find element by style selector in jquery

2020-02-26 05:32发布

I am trying to find an element with a particular style and change its style to something else.

Here is the html element

<table style='width: 555px;'>
  <tr>
    <td>blablabla</td>
  </tr>
</table>

Now I am trying to find the table with width 555px and change it to 650px using jquery.

$('table[style*=width:555px]').css("width", "610px");

But this is not working. Can somebody spark an idea please?

NOTE: For some reason I cannot change the html.

3条回答
仙女界的扛把子
2楼-- · 2020-02-26 06:02

Well the obvious issue is that your element is a <table> and your jQuery selector is selecting all <div> s.

I'd imagine there's also an issue with whitespace too, as your HTML contains spacing within the style element but your selector doesn't. (I may be wrong here, not experienced with the E[a*=v] selector.)

查看更多
乱世女痞
3楼-- · 2020-02-26 06:05
$('table').filter(function() {
    return $(this).css('width') == '555px';
}).css("width", "610px");
查看更多
走好不送
4楼-- · 2020-02-26 06:08

Beware of spaces :)

And you should quote the value with " or '.

$('table[style*="width: 555px"]').css("width", "610px");

If it does not work in IE, you could try to remove the space? (totally untested!)

$('table[style*="width: 555px"],table[style*="width:555px"]')
    .css("width", "610px");
查看更多
登录 后发表回答