简体   繁体   English

我想将“ text1”更改为“ text2”,第一次单击,然后将“ text2”更改为“ text1”,第二次单击只需单击一个按钮

[英]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.

相关问题 当 text1 和 text2 匹配时,如何控制日志? - How to console log something, when text1 and text2 are matched? 如何自动递增,如果我在 text1 上键入 2015,text2 应该显示 2016。有没有简单的方法? - how to auto increment, if I type 2015 on text1, text2 should display 2016. is there an easy way to this? 当我选中复选框时如何将值text1复制到text2? - When I check on checkbox how to copy value text1 to text2? 有人可以帮我制作一个循环,其中详细的文本和 state 每秒都会从 Text 更改为 Text2 并返回到 Text - Can someone help me make a loop where the text for detail and state change every second from Text to Text2 and back to Text jquery - 单击时更改按钮的文本仅在第二次单击时起作用 - jquery - change button's text on click works only on second click 点击更改按钮文字 - Change button text on click 我想在按钮上更改文本5次 - I want to change the text on a button 5 times when I click on it 如何在iframe中仅查找text2并向其中添加一个类 - How to find only text2 in iframe and add a class to it 我的 tailwindcss 没有按我的意愿工作,即我的 Text2 放置不正确 - My tailwindcss is not working as I want i.e my Text2 is not well placed 我需要在我的反应组件中为 text2 创建一个延迟 - I need to create a delay for text2 in my react component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM