使用Javascript | jQuery删除特定的内联样式 [英] Remove a specific inline style with Javascript|jQuery
本文介绍了使用Javascript | jQuery删除特定的内联样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的html中包含以下代码:
I have the following code in my html:
<p id='foo' style='text-align:center; font-size:14pt; font-family:verdana; color:red'>hello world</p>
以及我的外部CSS中的内容:
and that in my external css:
#foo{ font-size:11pt; font-family:arial; color:#000; }
我想删除样式"属性中的所有字体大小"和字体系列",但不删除颜色"和外部CSS中设置的其他字体.
I want to remove all "font-size" and "font-family" in the "style" atribute, but not the "color" and others setted in external css.
预期结果:
<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. Example:
elem.style.removeProperty('font-family');
当然,IE< 9不支持此功能,因此您必须使用
Of course, IE < 9 doesn't support this so you'll have to use
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');
}
这篇关于使用Javascript | jQuery删除特定的内联样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文