亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

怎么樣能讓“點擊更改”后“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>


正在回答

4 回答

<!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>
這是我仿照你之前提的問題,別人給出答案的方式寫的,雖然能實現你說的效果,但我覺得代碼有點冗余


1 回復 有任何疑惑可以回復我~
#1

大塊吃肉188 提問者

非常感謝!
2016-08-15 回復 有任何疑惑可以回復我~
#2

寶慕林5689754

nice
2019-08-24 回復 有任何疑惑可以回復我~

調用document.write就是重新打開一個文檔。本來你的這個頁面已經加載完畢,然后你在執行一個document.write重新打開一個文檔然后輸出,所以你原來的內容就會被覆蓋了

0 回復 有任何疑惑可以回復我~

已經改變了?。肯鹊綖g覽器執行看看是不是沒調用我直接復制你的代碼執行沒錯的

0 回復 有任何疑惑可以回復我~

我試著把document.write("P元素Class值為:"+mychar.className+"<br>");這句放到function modifyclass(){ ?。沁@樣會覆蓋上邊的內容

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

怎么樣能讓“點擊更改”后“p元素class值為:one”變為two?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號