Javascript jQuery鼠标单击计数器-如何重置?

Javascript jQuery鼠标单击计数器-如何重置?,javascript,jquery,events,click,reset,Javascript,Jquery,Events,Click,Reset,我正试图建立一个网站,计数每次用户点击鼠标。我已关闭该部分,但我还需要包括一个重置按钮,在用户单击该按钮后,从0开始鼠标单击计数器 我无法理解-如果我将var=0移动到doc ready,它会重置,但在单击按钮后,计数器永远不会超过1。我不知道该怎么办 我的剧本- $(document).ready(function(){ console.log("Here"); $(document).click(function(e) { $('#location').a

我正试图建立一个网站,计数每次用户点击鼠标。我已关闭该部分,但我还需要包括一个重置按钮,在用户单击该按钮后,从0开始鼠标单击计数器

我无法理解-如果我将var=0移动到doc ready,它会重置,但在单击按钮后,计数器永远不会超过1。我不知道该怎么办

我的剧本-

$(document).ready(function(){
    console.log("Here");


    $(document).click(function(e) {
        $('#location').append("("+e.clientX+", "+e.clientY+")<br>");
    });

    var counter = 0;
    $(document).click(function(e) {
        counter++;
        $("#mouseclick").text("Total mouse clicks: " + counter);
    });

    $('button').click(function(e) {
        e.stopPropagation();  // stop the event from propagating up the visual tree
        $('#location').text("");
        $("#mouseclick").text("Total mouse clicks: 0");
    });

    $('button')
});
$(文档).ready(函数(){
console.log(“此处”);
$(文档)。单击(函数(e){
$('#location')。追加(“+e.clientX+”,“+e.clientY+”)
”; }); var计数器=0; $(文档)。单击(函数(e){ 计数器++; $(“#鼠标点击”).text(“鼠标点击总数:+计数器); }); $(“按钮”)。单击(函数(e){ e、 stopPropagation();//停止事件向上传播到可视化树 $(“#位置”)。文本(“”); $(“#鼠标单击”).text(“鼠标单击总数:0”); }); $(“按钮”) });

我需要他们能够点击“按钮”,计数将重置。有什么建议吗?

您没有重置计数器。看

$(文档).ready(函数(){
console.log(“此处”);
$(文档)。单击(函数(e){
$('#location')。追加(“+e.clientX+”,“+e.clientY+”)
”; }); var计数器=0; $(文档)。单击(函数(e){ 计数器++; $(“#鼠标点击”).text(“鼠标点击总数:+计数器); }); $(“按钮”)。单击(函数(e){ e、 stopPropagation();//停止事件向上传播到可视化树 $(“#位置”)。文本(“”); 计数器=0; $(“#鼠标单击”).text(“鼠标单击总数:0”); });
只需在$(“按钮”)中添加计数器=0。单击()事件


第一个计数器=0;然后是$(“#鼠标单击”)。文本(“鼠标单击总数:“+计数器”);=)什么是“$('button')”最后?在你的按钮点击事件中,只需设置
counter=0
哦,我的天啊……这太简单了。我真的以为我已经尝试了所有的方法。完全想过头了,哈哈。谢谢!!谢谢!我以为我昨晚已经做了,但现在回想起来,我把var counter=0;而不仅仅是counter=0;。谢谢!事实上,如果我可以问的话-为什么var counter=0;不起作用,而不是counter=0;?这是因为变量已经定义,因此变得多余?如果使用
var
关键字重新定义变量,它通常会抛出错误。
$(document).ready(function(){
  console.log("Here");


$(document).click(function(e) {
  $('#location').append("("+e.clientX+", "+e.clientY+")<br>");
});

var counter = 0;
$(document).click(function(e) {
  counter++;
  $("#mouseclick").text("Total mouse clicks: " + counter);
});

$('button').click(function(e) {
  e.stopPropagation();  // stop the event from propagating up the visual tree
  $('#location').text("");
  counter = 0;
  $("#mouseclick").text("Total mouse clicks: 0");
});
$('button').click(function(e) {
   counter=0;
   e.stopPropagation();  // stop the event from propagating up the visual tree
   $('#location').text("");
   $("#mouseclick").text("Total mouse clicks: 0");
});