我的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:

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

我想删除style atribute中的所有font-sizefont-family,但不删除外部css中的color和其他设置.

Result expected:

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

100

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

推荐答案

对于那些不使用jQuery的用户,可以使用native 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');
}

Jquery相关问答推荐

bootstrap - Select Select 器不能在下拉菜单中 Select

Ajax 替换而不是追加

如何判断输入日期是否等于今天的日期?

可以在不使用for=id的情况下将标签与复选框相关联吗?

按百分比zoom div的内容?

如何使用 jQuery Select sibling 元素?

删除索引后的所有项目

在 jquery 中启用/禁用下拉框

通过 AJAX 和 jQuery 使用 HTML5 文件上传

如何使用 jquery 动态更改 iframe 中的内容?

jQuery:如何从 $.ajax.error 方法中获取 HTTP 状态代码?

我如何知道 jQuery 是否有待处理的 Ajax 请求?

javascript过滤对象数组

CSS3 相当于 jQuery slideUp 和 slideDown?

通过 AJAX MVC 下载 Excel 文件

jquery - 从一个非常大的表中删除所有行的最快方法

jquery $(window).width() 和 $(window).height() 在未调整视口大小时返回不同的值

AJAX 成功中的 $(this) 不起作用

jQuery:通过 .attr() 添加两个属性;方法

使用 jQuery 将一个标签替换为另一个标签