使用Javascript | jQuery删除特定的内联样式

阵风:

我的html中有以下代码:

<p id='foo' style='text-align:center; font-size:14pt; font-family:verdana; color:red'>hello world</p>

在我的外部CSS中:

#foo{ font-size:11pt; font-family:arial; color:#000; }

我想删除“样式”属性中的所有“字体大小”和“字体系列”,但不删除“颜色”和在外部CSS中设置的其他字体。

预期结果:

<p id='foo' style='text-align:center; color:red'>hello world</p>

已经尝试过:

$('#foo').removeAttr('style');   // That removes all inline
$('#foo').css('font-family',''); // That remove the css setted too
我:

对于不使用jQuery的用户,可以使用本机的removeProperty方法从内联样式中删除特定样式例:

elem.style.removeProperty('font-family');

当然,IE <9不支持此功能,因此您必须使用

elem.style.removeAttribute('font-family');

所以跨浏览器的方法是:

if (elem.style.removeProperty) {
    elem.style.removeProperty('font-family');
} else {
    elem.style.removeAttribute('font-family');
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章