功能是按下按钮,单词如果是good,就变成bad,如果是bad,就变成good
///////////////////////////////////////////////////////////////////////////////
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript">
function changeword(){
var newword=document.getElementsByClassName('word');
if (newword[0]=="good") {
newword[0]="bad";
}
else{
newword[0]="good";
}
}
</script>
<title>changeword</title>
</head>
<body>
<input type="button" value="changeword" id="abutton" onclick="changeword()">
<p class="word">good</p>
</body>
</html>
1 回答
已采纳
luofuxiang
TA贡献34条经验 获得超34个赞
标签里面的内容用属性innerHTML来表示,楼主的代码需要简单改一下,如下所示:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script type="text/javascript"> function changeword(){ var newword=document.getElementsByClassName('word'); if (newword[0].innerHTML=="good") { //修改 newword[0].innerHTML="bad"; ////修改 } else{ newword[0].innerHTML="good";////修改 } } </script> <title>changeword</title> </head> <body> <input type="button" value="changeword" id="abutton" onclick="changeword()"> <p class="word">good</p> </body> </html>
添加回答
举报
0/150
提交
取消