为什么这段javascript小代码无法实现词语替换

功能是按下按钮,单词如果是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>


newsudhc3634140
浏览 1284回答 1
1回答

luofuxiang

    标签里面的内容用属性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>
打开App,查看更多内容
随时随地看视频慕课网APP