Mat*_*att 92 javascript css jquery inline
如果我的html中有以下内容:
<div style="height:300px; width:300px; background-color:#ffffff;"></div>
Run Code Online (Sandbox Code Playgroud)
这在我的css样式表中:
div {
width:100px;
height:100px;
background-color:#000000;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法,使用javascript/jquery,删除所有内联样式,只留下由CSS样式表指定的样式?
Jos*_*ier 28
普通的JavaScript:
你不需要jQuery来做这样的微不足道的事情.只需使用该.removeAttribute()
方法.
假设您只是针对单个元素,您可以轻松使用以下内容:( 示例)
document.querySelector('#target').removeAttribute('style');
Run Code Online (Sandbox Code Playgroud)
如果您要定位多个元素,只需遍历选定的元素集合:( 示例)
var target = document.querySelectorAll('div');
Array.prototype.forEach.call(target, function(element){
element.removeAttribute('style');
});
Run Code Online (Sandbox Code Playgroud)
Array.prototype.forEach()
- IE9及以上/ .querySelectorAll()
- IE 8(部分)IE9及以上版本.
归档时间: |
|
查看次数: |
98554 次 |
最近记录: |