如果按钮单击 3 次则执行操作 [javascript]
Do action if button clicked 3 times [javascript]
<html>
<body>
<button onclick = 'click1()'> </button>
</body>
<script>
var one
function click1(){
one = one + 1;
}
if (one == 3){
document.write('yes')
}
</script>
</html>
这里是一个例子JS/HTML。如果单击按钮三次,我怎么写是?。此代码适用于 python 和其他语言。我如何在 JS 中执行此操作?
您的按钮上的函数调用似乎缺少括号。尝试:onclick='click1()'
像这样更改您的代码:
<html>
<body>
<button onclick="click1()">Click</button>
</body>
<script>
var one = 0;
const click1 = () => { if (++one === 3) document.write('yes') };
</script>
</html>
您的代码存在语法和逻辑错误
<html>
<body>
<button onclick='click1()'>click here </button>
<script>
var one = 0;
function click1(){
one = one + 1;
if (one == 3){
alert("here");
}
}
</script>
</body>
</html>
点击三下后,您必须再次在 if 语句
中重置变量 one
if (one == 3){
alert("here");
one = 0;
}
这里有多个问题。
- 首先你应该为变量设置一个默认值,否则它将被声明为未定义。
- 其次你应该把你的 if 放在同一个函数中。
- 第三,你应该在 html => 'click1()'
中用括号调用你的函数
我也建议做一些改变
- 将变量设为 let 而不是 var。
- 使用 3 个等号进行类型安全检查
<html>
<body>
<button onclick = 'click1()'> </button>
<script>
let one = 0;
function click1(){
one += 1;
if (one === 3){
document.write('yes')
}
}
</script>
</body>
</html>
var one = 0;
function click1() {
one++
if (one == 3) {
console.log('yes');
one = 0
}
}
<button onclick ='click1()'> </button>
您可以使用如下方法。详情见代码注释:
// Reference to the DOM elements
const countEl = document.querySelector('#count-el');
const buttonEl = document.querySelector('button');
// Init a counter variable and return a function that increments it
const increment = (() => {
let i = 1;
return () => i++;
})();
// The click event listener
buttonEl.addEventListener('click', () => {
// Increment the count and update the UI based on if current click count is 3
if (increment() === 3) {
countEl.textContent = 'yes';
}
});
<span id="count-el"></span>
<br />
<button>Click 3 times</button>
<html>
<body>
<button onclick = 'click1()'> </button>
</body>
<script>
var one
function click1(){
one = one + 1;
}
if (one == 3){
document.write('yes')
}
</script>
</html>
这里是一个例子JS/HTML。如果单击按钮三次,我怎么写是?。此代码适用于 python 和其他语言。我如何在 JS 中执行此操作?
您的按钮上的函数调用似乎缺少括号。尝试:onclick='click1()'
像这样更改您的代码:
<html>
<body>
<button onclick="click1()">Click</button>
</body>
<script>
var one = 0;
const click1 = () => { if (++one === 3) document.write('yes') };
</script>
</html>
您的代码存在语法和逻辑错误
<html>
<body>
<button onclick='click1()'>click here </button>
<script>
var one = 0;
function click1(){
one = one + 1;
if (one == 3){
alert("here");
}
}
</script>
</body>
</html>
点击三下后,您必须再次在 if 语句
中重置变量one
if (one == 3){
alert("here");
one = 0;
}
这里有多个问题。
- 首先你应该为变量设置一个默认值,否则它将被声明为未定义。
- 其次你应该把你的 if 放在同一个函数中。
- 第三,你应该在 html => 'click1()' 中用括号调用你的函数
我也建议做一些改变
- 将变量设为 let 而不是 var。
- 使用 3 个等号进行类型安全检查
<html>
<body>
<button onclick = 'click1()'> </button>
<script>
let one = 0;
function click1(){
one += 1;
if (one === 3){
document.write('yes')
}
}
</script>
</body>
</html>
var one = 0;
function click1() {
one++
if (one == 3) {
console.log('yes');
one = 0
}
}
<button onclick ='click1()'> </button>
您可以使用如下方法。详情见代码注释:
// Reference to the DOM elements
const countEl = document.querySelector('#count-el');
const buttonEl = document.querySelector('button');
// Init a counter variable and return a function that increments it
const increment = (() => {
let i = 1;
return () => i++;
})();
// The click event listener
buttonEl.addEventListener('click', () => {
// Increment the count and update the UI based on if current click count is 3
if (increment() === 3) {
countEl.textContent = 'yes';
}
});
<span id="count-el"></span>
<br />
<button>Click 3 times</button>