如果等于大于5,则使用Javascript

Javascript if equals higher than 5

本文关键字:Javascript 于大于 大于 如果      更新时间:2023-11-04

所以我有一个计数器设置,如果计数超过5,我想让它自动点击报告按钮,这就是我目前所拥有的,但它似乎不起作用。

if(counter > 5){
document.getElementById("report-post-submit").click();
}

有人知道为什么它不起作用吗?

更新为完整代码:

var counter = 0
var timer;
function countUP () {
 counter = counter + 1;//increment the counter by 1
 //display the new value in the div
 document.getElementById("timer_container").innerHTML = counter;
}
    if(counter > '5'){
document.getElementById("wp-report-post-submit").click();
}

<body onload='timer=setInterval("countUP()", 1000 );'>
<div id="timer_container">0</div> 
<a class="report-post-button" id="report-post-submit"><?php _e("Send Report"); ?></a>

你做错了。

检查注释行:

var counter = 0
var timer;
function countUP () {
  // set onclick event if it's not appended to the element:
  // document.getElementById("report-post-submit").onclick = function(){
  //   doStuff('Stuff done!');
  // }
 
  //increment the counter by 1
  counter++;
  //display the new value in the div
  document.getElementById("timer_container").innerHTML = counter;
  
  // Move this into 'countUP' function, to check counter while counting (not on page load time):
  if(counter > 5){    
    // trigger click event applied to the button:
    // document.getElementById("report-post-submit").onclick();
    // or simply call the method right here:
    doStuff('Stuff done!');
  }
}
function doStuff(txt){
  // your function to be fired on click or when counter gets more than 5:
  alert(txt);
  // reset counter after the stuff is done:
  counter = 0;
  // or clear the interval:
  // clearInterval(timer);
}
<body onload='timer=setInterval(countUP, 1000 );'>
  <div id="timer_container">0</div> 
  <!-- append 'onclick' event to the button (or set the event in script section) -->
  <a class="report-post-button" onclick="doStuff('Stuff done!');" id="report-post-submit">do stuff</a>
</body>