[英]i want to change “text1” to “text2” first click and change “text2” to “text1” second click by only single button
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>
i want to change "text1" to "text2" first click and change "text2" to "text1" second click by only single button. 我想将“ text1”更改为“ text2”,第一次单击,然后将“ text2”更改为“ text1”,第二次单击只需单击一个按钮。
You are missing return statement: 您缺少返回声明:
function fun(){
var read1 = document.getElementById("ddr");
if(read1.innerHTML == "text2"){
read1.innerHTML = "text1";
return; //return here
}
read1.innerHTML = "text2"
}
or with else
: 或与
else
:
function fun(){
var read1 = document.getElementById("ddr");
if(read1.innerHTML == "text2"){
read1.innerHTML = "text1";
} else {
read1.innerHTML = "text2"
}
}
You just missed the else
statement. 您只是错过了
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.