[英]Remove a specific inline style with Javascript|jQuery
I have the following code in my html:我的 html 中有以下代码:
<p id='foo' style='text-align:center; font-size:14pt; font-family:verdana; color:red'>hello world</p>
and that in my external css:而在我的外部 css 中:
#foo{ font-size:11pt; font-family:arial; color:#000; }
I want to remove all font-size
and font-family
in the style
atribute, but not the color
and others set in external css.我想删除style
属性中的所有font-size
和font-family
,但不删除外部 css 中设置的color
和其他内容。
Result expected:预期结果:
<p id='foo' style='text-align:center; color:red'>hello world</p>
Already tried:已经尝试过:
$('#foo').removeAttr('style'); // That removes all inline
$('#foo').css('font-family',''); // That remove the css setted too
For those that aren't using jQuery, you can delete specific styles from the inline styles using the native removeProperty method.对于那些不使用 jQuery 的人,您可以使用本机removeProperty方法从内联样式中删除特定样式。 Example:示例:
elem.style.removeProperty('font-family');
Of course, IE < 9 doesn't support this so you'll have to use当然,IE < 9 不支持这个,所以你必须使用
elem.style.removeAttribute('font-family');
so a cross browser way to do it would be:所以跨浏览器的方式是:
if (elem.style.removeProperty) {
elem.style.removeProperty('font-family');
} else {
elem.style.removeAttribute('font-family');
}
将属性设置为inherit
:
$('#foo').css('font-family','inherit').css('font-size','inherit');
I think there is no proper solution to this problem (without changing your markup).我认为这个问题没有合适的解决方案(不改变你的标记)。 You could search and replace the style attribute's value:您可以搜索和替换样式属性的值:
var element = $('#foo');
element.attr('style', element.attr('style').replace(/font-size:[^;]+/g, '').replace(/font-family:[^;]+/g, ''))
By far the best solution would be to get rid of the inline styles and manage the styles by using classes.到目前为止,最好的解决方案是摆脱内联样式并通过使用类来管理样式。
In 2019, the simplest way to remove a property seems to be:在 2019 年,删除属性的最简单方法似乎是:
elem.style.border = "";
Similarly, to set a border:同样,要设置边框:
elem.style.outline = "1px solid blue";
Should work across all browsers too!也应该适用于所有浏览器!
My suggestion would be to stay away from setting this stuff using inline styles.我的建议是不要使用内联样式设置这些东西。 I would suggest using classes and then using jQuery to switch between them:我建议使用类,然后使用 jQuery 在它们之间切换:
CSS: CSS:
#foo{ font-size:11pt; font-family:arial; color:#000; }
#foo.highlight {text-align:center; font-size:14pt; font-family:verdana; color:red}
HTML: HTML:
<p id="foo" class="highlight">hello world</p>
Javascript: Javascript:
$('#foo').removeClass('highlight');
$('#foo').addClass('highlight');
dynamic add and remove inline style property动态添加和删除内联样式属性
var check=()=>{ console.log('test') var el=document.getElementById("id"); var condition=el.style['color']!=""?true:false; if(condition){ el.style.removeProperty('color') } else{ el.style.color="red" } }
<div id="id" style="display:block">dynamic style</div> <button onclick="check()">apply style</button>
From what I can see you really need two different styles for the paragraph.从我所看到的,你真的需要两种不同的段落样式。 It might be easier to set those up in CSS and then just use jQuery to remove / add as you need:在 CSS 中设置它们可能更容易,然后根据需要使用 jQuery 删除/添加:
#styleOne { color: red; font: normal 14pt Verdana; text-align: center; }
#styleTwo{ color: #000; font: normal 11pt Arial; text-align: center; }
Your initial html will look like:您的初始 html 将如下所示:
<p id="styleOne">hello world</p>
And then to revert to styleTwo in jQuery然后在 jQuery 中恢复为 styleTwo
$('p#styleOne').removeClass('styleOne').addClass('styleTwo');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.