如何使用javascript删除所有内联样式,只留下在css样式表中指定的样式? [英] How can I erase all inline styles with javascript and leave only the styles specified in the css style sheet?

查看:188
本文介绍了如何使用javascript删除所有内联样式,只留下在css样式表中指定的样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在我的HTML中有以下内容:

If I have the following in my html:

<div style="height:300px; width:300px; background-color:#ffffff;"></div>

在我的css样式表中:

And this in my css style sheet:

div {
    width:100px;
    height:100px;
    background-color:#000000;
}

有什么办法,使用javascript / jquery删除所有的inline样式,只留下由css样式表指定的样式?

Is there any way, with javascript/jquery, to remove all of the inline styles and leave only the styles specified by the css style sheet?

推荐答案

$ .attr('style','');

code> $('div')。removeAttr('style'); (From Andres的回答

$('div').removeAttr('style'); (From Andres's Answer)

要使它变小一点,请尝试:

To make this a little smaller, try this:

$('div [style]')。removeAttr('style');

这样会加快一点,因为它会检查div是否有style属性。

This should speed it up a little because it checks that the divs have the style attribute.

无论如何,如果你有大量的div,这可能需要一些时间来处理,所以你可能想考虑除javascript之外的其他方法。

Either way, this might take a little while to process if you have a large amount of divs, so you might want to consider other methods than javascript.

这篇关于如何使用javascript删除所有内联样式,只留下在css样式表中指定的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆