使用 javascript 创建一个开/关按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6089593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:25:03  来源:igfitidea点击:

Creating an ON/OFF button with javascript

javascript

提问by saeed

Place a single button on a page that could be used as an ON/OFF switch. Design this button so that the button is labeled "ON". When you click on the button its label should change to "OFF". Clicking on it again should change it back to "ON" and so on.

在页面上放置一个可用作 ON/OFF 开关的按钮。设计此按钮,以便将按钮标记为“ON”。当您单击按钮时,其标签应更改为“关闭”。再次单击它应该将其更改回“ON”等。

回答by

This ok?:

这好吗?:

<script type="text/javascript">
function onoff(){
  currentvalue = document.getElementById('onoff').value;
  if(currentvalue == "Off"){
    document.getElementById("onoff").value="On";
  }else{
    document.getElementById("onoff").value="Off";
  }
}
</script>

<input type="button" value="On" id="onoff" onclick="onoff();">