【问题标题】:Clear text if button is clicked如果单击按钮,则清除文本
【发布时间】:2014-06-25 05:04:11
【问题描述】:

我需要创建一个页面,显示页面上任意位置的点击次数以及点击发生位置的列表。我还有一个清除列表的按钮。按钮不清除。按钮点击被计为对页面正文的点击,这会增加计数并且永远不会清除它。

HTML:

<p><h3>Click count:<span id="output">
                </span></h3></p>


                <h3>Click locations:</h3>
                <div id="clickLocations"></div> 
        <input id="btn" type="button" value="Validate">

<script>
$(document).ready(function() {
    var count = 0;
    $("body").click(function (event) {

        count += 1; //Count clicks when the body is clicked
        if (count > 0) {
            $("span#output").html(" " + count);
            $("div#clickLocations").append("<ul><li>" + event.pageX + ", " + event.pageY + "</li></ul>"); //Display the x,y location of the click

        }
        $('#btn').click(function () { //Clear text when the button is clicked
            $("div#clickLocations").html('');
            $("span#output").empty();

            event.stopPropagation();
        }); //End button.click 
    }); //End body.click
}); //End document.ready

</script>

【问题讨论】:

标签: jquery html


【解决方案1】:

您正在将您的点击绑定到“body”,它不会记录浏览器窗口中任何地方的点击,只会在已经生成的 DOM 中记录(因此在您的情况下,它不会记录点击,例如在验证按钮下方)。最好绑定 do 'document' 来捕获整个窗口中的点击。

如果您不想计算按钮的点击次数,则需要检查点击来源并简单地对这些点击进行打折。单击验证后,您可能还想重置计数器。

这里是代码

$(document).ready(function() {
    var count = 0;
    //note this will capture events everywhere in the window
    $(document).click(function (event) {

        //do not count the button click
        if (event.target.id=="btn") {
            $("div#clickLocations").html('');
            $("span#output").empty();
            //reset counter
            count = 0;
            event.stopPropagation();
        } else {
            count += 1; //Count clicks when the body is clicked
            if (count > 0) {
                $("span#output").html(" " + count);

                // Display the x,y location of the click
                $("div#clickLocations").append("<ul><li>" + event.pageX + ", "
                                   + event.pageY + "</li></ul>");
            }
        }
    }); //End body.click
}); //End document.ready

And the demo

【讨论】:

  • 请注意,if(count &gt; 0) 是无用的。 count 设置为零,并在测试前增加 1。所以它永远是正数(特别是因为数字是 JavaScript 中的浮点数,所以它们不会像整数一样环绕。)
【解决方案2】:

将一个事件处理程序绑定到另一个事件处理程序中几乎总是错误的。这会导致将多个处理程序添加到内部元素,因为每次触发外部处理程序时都会重复它。代码中的另一个问题是内部处理程序中的event 指的是触发外部处理程序的事件,因为内部处理程序没有event 参数。

试试这个:

$(document).ready(function() {
    var count = 0;
    $("body").click(function (event) {

        count += 1; //Count clicks when the body is clicked
        if (count > 0) {
            $("span#output").html(" " + count);
            $("div#clickLocations").append("<ul><li>" + event.pageX + ", " + event.pageY + "</li></ul>"); //Display the x,y location of the click

        }
    }); //End body.click
    $('#btn').click(function (event) { //Clear text when the button is clicked
        $("div#clickLocations").html('');
        $("span#output").empty();

        event.stopPropagation(); // Prevent button click from being counted
    }); //End button.click 
}); //End document.ready

DEMO

顺便说一句,html('')empty() 做同样的事情。

【讨论】:

  • 请注意,您的示例代码在另一个事件处理程序中附加了 2 个 click 事件处理程序:ready。只是说... 8-) 也与其他条目相同的评论,if(count &gt; 0) 不是必需的。
  • 由于ready 处理程序只触发一次,这不是问题。
  • 请注意我说的是“几乎总是”。这是明显的例外。我几乎不认为ready 是一个事件处理程序,它更像是确保代码在正确的时间运行的样板。
猜你喜欢
  • 2014-02-27
  • 1970-01-01
  • 2012-02-04
  • 1970-01-01
  • 2013-04-20
  • 2015-06-13
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
相关资源
最近更新 更多