为什么不removeAttribute()删除以下代码中的任何内容:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<body>
<div id="myDiv">
Element with style.
</div>
<br />
<br />
<button onclick="DelStyle()">
Remove the style attribute from the element
</button>
<script type="text/javascript">
function DelStyle() {
var div = document.getElementById("myDiv");
console.log(div)
div.style.border = 'solid #3B3B3D 1px';
console.log(div)
div.removeAttribute("style");
console.log(div)
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
问题是您在 DOM 更新之前立即进行所有这些更改。相反,请考虑实际将样式属性设置为无,请参阅此处
div.setAttribute("style","");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26833 次 |
| 最近记录: |