function fun(){
var read1 = document.getElementById("ddr");
if(read1.innerHTML == "text2"){
read1.innerHTML = "text1";
}
read1.innerHTML = "text2"
}

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<button id="bt" onclick="fun()"> button </button>
<p id="ddr">
text1
</p>
</body>
</html>
&#13;
我想改变&#34; text1&#34;到&#34; text2&#34;首先点击并更改&#34; text2&#34; to&#34; text1&#34;只需按一下按钮。
答案 0 :(得分:0)
您缺少退货声明:
function fun(){
var read1 = document.getElementById("ddr");
if(read1.innerHTML == "text2"){
read1.innerHTML = "text1";
return; //return here
}
read1.innerHTML = "text2"
}
或else
:
function fun(){
var read1 = document.getElementById("ddr");
if(read1.innerHTML == "text2"){
read1.innerHTML = "text1";
} else {
read1.innerHTML = "text2"
}
}
答案 1 :(得分:0)
您错过了else
声明。
function fun(){
var read1 = document.getElementById("ddr");
if(read1.innerHTML == "text2"){
read1.innerHTML = "text1";
}
else{
read1.innerHTML = "text2"
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<button id="bt" onclick="fun()"> button </button>
<p id="ddr">
text1
</p>
</body>
</html>