怎么樣能讓“點擊更改”后“p元素class值為:one”變為two?
<!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
<!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> <p?id="aa"></p> <form> <input?type="button"?name="button"?onclick="modifyclass()"?value="點擊更改"?/> </form> <script?type="text/javascript"> var?mychar=document.getElementById("con"); var?aa=document.getElementById("aa"); aa.innerHTML=("P元素Class值為:"+mychar.className+"<br>"); function?modifyclass(){ mychar.className="two"; aa.innerHTML=("P元素Class值為:"+mychar.className+"<br>"); } </script> </body> </html> 這是我仿照你之前提的問題,別人給出答案的方式寫的,雖然能實現你說的效果,但我覺得代碼有點冗余2016-08-06
調用document.write就是重新打開一個文檔。本來你的這個頁面已經加載完畢,然后你在執行一個document.write重新打開一個文檔然后輸出,所以你原來的內容就會被覆蓋了
2016-08-06
已經改變了?。肯鹊綖g覽器執行看看是不是沒調用我直接復制你的代碼執行沒錯的
2016-07-26
我試著把document.write("P元素Class值為:"+mychar.className+"<br>");這句放到function modifyclass(){ ?。沁@樣會覆蓋上邊的內容