jQuery 在jquery中按样式选择器查找元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3841850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:13:15  来源:igfitidea点击:

Find element by style selector in jquery

jqueryjquery-selectors

提问by Krishna

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

我试图找到具有特定样式的元素并将其样式更改为其他样式。

Here is the html element

这是html元素

<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.

现在我试图找到宽度为 555px 的表格并使用 jquery 将其更改为 650px。

$('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.

注意:由于某种原因,我无法更改 html。

回答by Vincent Robert

Beware of spaces :)

当心空格:)

And you should quote the value with "or '.

并且您应该用"or引用该值'

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

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

如果它在 IE 中不起作用,您可以尝试删除空间吗?(完全未经测试!)

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

回答by melhosseiny

$('table').filter(function() {
    return $(this).css('width') == '555px';
}).css("width", "610px");

回答by chigley

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

那么显而易见的问题是您的元素是 a<table>并且您的 jQuery 选择器正在选择所有<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.)

我想空格也存在问题,因为您的 HTML 在样式元素中包含间距,但您的选择器没有。(我可能在这里错了,对E[a*=v]选择器没有经验。)