[英]Two events on the same button with two click
I need to create a button that works like this : 我需要创建一个像这样的按钮:
var i = true
first click --> var i = false
second click --> var i = true
....
HTML HTML
<input type="button" value="test" onclick="stop(); start();" />
How can i specify theese functions in my JS document ? 如何在JS文档中指定theese函数?
you can toggle a boolean by doing this : 您可以通过执行以下操作来切换布尔值:
var b = true;
b = !b
in your case use : 在您的情况下使用:
<input type="button" value="test" onclick="b = !b;" />
it's better to doing this with a function 最好用一个函数来做到这一点
var b = true;
function toggle () { b = !b; console.log(b) }
and in your html 并在您的html中
<input type="button" value="test" onclick="toggle();" />
make a counter for clicks 为点击计数
var countClick= 0 if (countClick== 1) { //do the first click code } if (countClick== 2) { //do the second click code }
You can do it like this 你可以这样
<input type="button" value="test" />
And the javascript code. 和javascript代码。
var btn = document.getElementsByTagName('input')[0];
var i = true;
btn.addEventListener('click', function() {
if (i == true)
i = false;
else
i = true;
});
You can simply associate a function call on onclick
event and then toggle the boolean
value: 您可以简单地将onclick
事件上的函数调用关联起来,然后切换boolean
值:
var i = true; function clicked () { //toggle the value i = !i; console.log(i); }
<input type="button" value="test" onclick="clicked();" />
Here is a snippet that does what you want. 这是一个片段,可以满足您的需求。 You can have it toggle forever or just the one time like your example. 您可以像示例一样永久地或一次地切换它。
var buttonClicks = 0; var boolValue = true; var boolValueOutput = document.getElementById("boolValue") boolValueOutput.innerHTML = boolValue; function onButtonClick() { buttonClicks++; // If you want it to only work once uncomment this //if (buttonClicks > 2) // return; boolValue = !boolValue; boolValueOutput.innerHTML = boolValue; }
<input type="button" value="test" onclick="onButtonClick();" /> <p id="boolValue"></p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.