简体   繁体   English

停用点击事件的最佳方法是

[英]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() ,该链接仅允许单击一次链接,请参见以下代码。

one() API doc one()API文档

<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.

相关问题 在javascript中切换点击事件的最佳方法是什么? - What is the best way to toggle a click event in javascript? 使用 DOM 单击事件停用链接 - Deactivate link with DOM click event 什么是最好的方式来显示每次点击从jQuery数据3项 - What is the best way to show 3 items per click from jquery data 在点击事件中传递敏感变量的最佳方法? - Best way to pass sensitive variables on click event? 停用最后一列的click-Event - Deactivate click-Event for last column 除了如图所示的框外,在 DOM 正文中绑定单击事件的最佳/优化方法是什么? - What is the best/optimized way to bind click event in DOM body except the box as shown in figure? 动态创建新 Vimeo 播放器列表并添加事件以在每次单击按钮时暂停的最佳方法是什么? - What is the best way to dynamically create a list of new Vimeo players and add an event to pause each on button click? 在IE9 beta中阻止点击事件传播的最佳方法是什么? - What's the best way to stop a click event propagating in IE9 beta? 响应按钮单击事件,使图像显示在特定div中的最佳方法是什么? - What is the best way to make an image appear in a specific div in response to a button click event? 在JavaScript中添加事件的最佳方法是什么? - What is the best way to add an event in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM