爲什麼在下面的代碼不會removeAttribute()
刪除任何東西:的removeAttribute()不工作DOM
<!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>
在'div.style.border = '#實1px的3B3B3D';'你不設置元素的樣式*屬性*,你只設置該對象的樣式*屬性*。 – arnaud576875
從@ arnaud576875的評論繼,您可以用'setAttribute'然後'removeAttribute'將工作:http://jsfiddle.net/interdream/tFxMm/1/ –