怎么樣能讓“點擊更改”后“p元素class值為:one”變?yōu)閠wo?
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8">
<title>className屬性</title>
<style type="text/css">
input{
font-size: 10px;
}
.one{
width:200px;
background-color: #CCC;
}
.two{
font-size: 18px;
color:#F00;
}
</style>
</head>
<body>
<p id="con" class="one">JavaScript</p>
<form>
<input type="button" name="button" onclick="modifyclass()" value="點擊更改" />
</form>
<script type="text/javascript">
var mychar=document.getElementById("con");
document.write("P元素Class值為:"+mychar.className+"<br>");
function modifyclass(){
mychar.className="two";
}
</script>
</body>
</html>
2016-07-29
2016-08-06
調(diào)用document.write就是重新打開一個文檔。本來你的這個頁面已經(jīng)加載完畢,然后你在執(zhí)行一個document.write重新打開一個文檔然后輸出,所以你原來的內(nèi)容就會被覆蓋了
2016-08-06
已經(jīng)改變了啊?先到瀏覽器執(zhí)行看看是不是沒調(diào)用我直接復制你的代碼執(zhí)行沒錯的
2016-07-26
我試著把document.write("P元素Class值為:"+mychar.className+"<br>");這句放到function modifyclass(){ ?。沁@樣會覆蓋上邊的內(nèi)容