【发布时间】: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>
【问题讨论】:
-
你能把jsFiddle放在一起吗?
-
您真的希望每次单击正文时都添加另一个
$('#btn')单击处理程序吗? -
你不能只用
$("span#output").html('');吗? -
这是他的原始代码:jsfiddle.net/barmar/PdbuS/2
-
试试这个链接。我想这就是你要找的。 stackoverflow.com/questions/15336316/…