[英]What is the best way to deactivate a click event from an
On my webpage I have a container that turns into something else when clicked, and then my intent was for it to stay the way it is after clicked: 在我的网页上,我有一个容器,该容器在单击时会变成其他东西,然后我的意图是使其保持在单击后的状态:
<div id="macheps-holder">
<p>Click here to see your browser's machine epsilon!</p>
<script type="text/javascript">
$(document).ready()
{
var temp1 = 1.0, temp2, macheps;
do {
macheps = temp1
temp1 /= 2
temp2 = 1.0 + temp1
} while (temp2 > 1.0)
var mh = $('#macheps-holder');
mh.click(function()
{
mh.html("<p>Your browsers machine epsilon is <code>" + macheps + "</code></p>");
mh.click(function()
{
return;
})
});
}
</script>
</div>
Also you can see, what I did was make the body of the click
function to change the click
function to return;
您还可以看到,我所做的是使
click
函数的主体更改为click
函数return;
, ie do nothing. ,即什么也不做。 I'm wondering if there's a more elegant and proper way of accomplishing this behavior.
我想知道是否有一种更优雅,更适当的方式来完成此行为。
Thank you for your time. 感谢您的时间。
What you have done, is not doing what you think it is doing... In each click the first click handler will get executed that will reset the html of the element with the same text then will add a new empty click handler... next click onwards will execute the first handler as well as the newly added blank handler... So on the 4th click you will have the first handler executed once and the blank handler executed 3 times 您已完成的操作并未按照您想的做...在每次单击中,将执行第一个单击处理程序,该操作将使用相同的文本重置元素的html,然后添加一个新的空单击处理程序...下次单击将执行第一个处理程序以及新添加的空白处理程序...因此,在第4次单击上,将使第一个处理程序执行一次,空白处理程序执行3次
Problem 问题
$(document).ready(function() { var temp1 = 1.0, temp2, macheps, counter; do { macheps = temp1 temp1 = temp1 / 2 temp2 = 1.0 + temp1 } while (temp2 > 1.0) var mh = $('#macheps-holder'); mh.click(function() { counter = 0; log('default handler'); mh.html("<p>Your browsers machine epsilon is <code>" + macheps + "</code></p>"); mh.click(function() { log('click: ' + counter++) return; }) }); }) var log = (function() { var $log = $('#log'); return function(msg) { $('<p/>', { text: msg }).appendTo($log) } })();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="macheps-holder"> <p>Click here to see your browser's machine epsilon!</p> </div> <div id="log"></div>
Since you want the click handler to get executed only once, use .one() 由于您只希望单击处理程序执行一次,因此请使用.one()
$(document).ready(function () {
var temp1 = 1.0,
temp2, macheps;
do {
macheps = temp1
temp1 /= 2
temp2 = 1.0 + temp1
} while (temp2 > 1.0)
var mh = $('#macheps-holder');
mh.one('click', function () {
mh.html("<p>Your browsers machine epsilon is <code>" + macheps + "</code></p>");
});
})
At first the jQuery's ready
function is not correct. 首先,jQuery的
ready
函数不正确。 You should pass a callback
to it, like this: 您应该将
callback
给它,如下所示:
$(document).ready(function() { ... })
The second. 第二。 You can create a separate function
callback
for the click
event and add or remove from event listener list
. 您可以为
click
事件创建一个单独的函数callback
,并从event listener list
添加或删除。 So you can write the following: 因此,您可以编写以下代码:
function clickHandler(event) { ... }
// add
mh.on('click', clickHandler);
// remove
mh.off('click', clickHandler);
The final code will look like this: 最终代码如下所示:
$(document).ready(function() { var temp1 = 1.0, temp2, macheps; do { macheps = temp1 temp1 /= 2 temp2 = 1.0 + temp1 } while (temp2 > 1.0) var mh = $('#macheps-holder'); function clickHandler() { mh.html("<p>Your browsers machine epsilon is <code>" + macheps + "</code></p>"); mh.off('click', clickHandler); // remove click event handler } mh.on('click', clickHandler); // add click event handler })
You can read more about on and off functions of jQuery. 您可以阅读有关jQuery 开启和关闭功能的更多信息。
You can make use of .one()
here that will allow to click your link only once, see below code. 您可以在此处使用
.one()
,该链接仅允许单击一次链接,请参见以下代码。
<script type="text/javascript">
$(document).ready()
{
var temp1 = 1.0, temp2, macheps;
do {
macheps = temp1
temp1 /= 2
temp2 = 1.0 + temp1
} while (temp2 > 1.0)
var mh = $('#macheps-holder');
mh.one('click',function()
{
mh.html("<p>Your browsers machine epsilon is <code>" + macheps + "</code></p>");
});
}
i hope u are looking for the click function to work for only first time ' 我希望您正在寻找只能在第一次使用的点击功能'
for that u can take a global variable 因为你可以接受一个全局变量
var count = 0;
mh.click(function()
{
++count;
if(count>=1)
{
return ;
}
mh.html("<p>Your browsers machine epsilon is <code>" + macheps + "</code></p>");
}); });
your codes have some problems. 您的代码有一些问题。 try this:
尝试这个:
$(document).ready(function() { var temp1 = 1.0, temp2, macheps; do { macheps = temp1 temp1 /= 2 temp2 = 1.0 + temp1 } while (temp2 > 1.0) var mh = $('#macheps-holder'); mh.click(function(e) { mh.html("<p>Your browsers machine epsilon is <code>" + macheps + "</code></p>"); }); })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <div id="macheps-holder"> <p>Click here to see your browser's machine epsilon!</p> </div>
Do it in a simple way like this 像这样简单地做
function DoThisClick() { //logic for clicking $("#yourbuttonid").unbind("click"); } $("#yourbuttonid").bind("click",Dothis());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.