我已經寫了這個代碼片,這在FF和Chrome中可以正常工作,但在IE中不能。爲什麼?爲什麼這個類的任務不在IE中工作
<html>
<header>
<style type="text/css">
.red{
color: red;
}
.green{
color: green;
}
.yellow{
color: yellow;
}
</style>
</header>
<body>
<div id="mydiv" style="height: 50px">Some contents</div>
<div>
<input type="radio" value="1" name="change" onclick="onClick(this)">Red</input>
<input type="radio" value="2" name="change" onclick="onClick(this)">Green</input>
<input type="radio" value="3" name="change" onclick="onClick(this)">Yellow</input>
</div>
<script type="text/javascript">
function onClick(el){
var className = "";
if(el.value == 1){
className = "red";
}else if(el.value == 2){
className = "green";
}else if(el.value == 3){
className = "yellow";
}
document.getElementById("mydiv").setAttribute("class", className);
}
</script>
</body>
</html>
或者甚至可能.className + =「」+ className; ? – danjah 2010-12-22 03:37:05
@Danjah但在這種情況下,我們必須刪除已分配的類。所以我們需要得到所有分配的類,然後刪除以前的類然後添加當前類。 – 2010-12-22 03:39:05