使用 jquery 删除特定的内联样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3819243/
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
Remove specific inline style with jquery
提问by Evan
There's an asp menu I'm using that is auto inserting style="width:3px;" into my menu table tds creating a nasty gab in between my tabs. I'm testing to remove this inline style with jquery instead of our developer customizing the menu just for this cosmetic blemish.
我正在使用一个 asp 菜单,它是自动插入 style="width:3px;" 进入我的菜单表 tds 在我的选项卡之间创建一个令人讨厌的对话。我正在测试使用 jquery 删除此内联样式,而不是我们的开发人员仅为此外观瑕疵自定义菜单。
below is a simple example:
下面是一个简单的例子:
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td style="width:3px;">hello world</td>
</tr>
</table>
in jquery, i can remove all tds with the style attribute by doing:
在 jquery 中,我可以通过执行以下操作删除所有带有 style 属性的 tds:
$('td').removeAttr('style');
so, my question is, how can i target the inline style that only contains 3px?
所以,我的问题是,如何定位仅包含 3px 的内联样式?
Here's my live demo: http://jsfiddle.net/n9upF/
这是我的现场演示:http: //jsfiddle.net/n9upF/
回答by Brian Kim
You are asking how you can select td's that have style attribute with only width:3px;
in it, right?
您在问如何选择仅具有样式属性的 td width:3px;
,对吗?
You can use Attribute Equals Selector.
您可以使用Attribute Equals Selector。
$("td[style='width:3px;']").removeAttr("style");?
回答by Nik Chankov
I believe that Evan wanted to remove only the width:3px; from the style while the other css styling remain in the attribute. So here is the solution:
我相信 Evan 只想删除 width:3px; 来自样式,而其他 css 样式保留在属性中。所以这里是解决方案:
$('td').each(function(){
if ($(this).attr('style').indexOf('3px') !== -1){
var style = $(this).attr('style');
$(this).attr('style', style.replace(/width: ?3px;/g, ''));
}
});
Working example is here
工作示例在这里
If this is not what you needed then the Sarfraz is shown the proper solution. :)
如果这不是您所需要的,那么将向 Sarfraz 显示正确的解决方案。:)
回答by Sarfraz
so, my question is, how can i only target the inline style that contains only 3px?
所以,我的问题是,我怎样才能只定位仅包含 3px 的内联样式?
Try this:
尝试这个:
$('td').each(function(){
if ($(this).attr('style').indexOf('3px') !== -1){
$(this).removeAttr('style');
}
});