style和className

来源:互联网 发布:百度知道 知乎 区别 编辑:程序博客网 时间:2024/06/05 07:42

元素.style.属性 = xxx,是修改行间样式,之后再修改className就会没有效果.

先变红后变绿色就没有问题,但是先变绿色再变红色就会出现问题,style的属性比className的优先级高。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Untitled Document</title></head><style>#div1 {width:200px; height:200px; border:1px solid black;}.box {background:red;}</style><script>function toRed(){var oDiv = document.getElementById('div1');//oDiv.style.background="red"oDiv.className = 'box';}function toGreen(){var oDiv = document.getElementById('div1');oDiv.style.background = 'green';}</script> <body><input type="button" value="变红" onclick="toRed()"/><input type="button" value="变绿" onclick="toGreen()"/><div id="div1"></div></body></html>


0 0