【问题标题】:jquery unbind function event once executedjquery unbind 函数事件一旦执行
【发布时间】:2011-09-26 10:46:49
【问题描述】:

大家好, 请帮我解决这个问题:假设我们从锚点调用相同的 javascript 函数,但参数不同

<a id="edit1" onclick="foo(1)"></a> 
<a id="edit2" onclick="foo(2)"></a>

function foo(id)
{
// let's say the code here hide the clicked anchor and show a new anchor with cancel-edit id 
}

场景:

  • 我点击了第一个锚点 - 第一个锚点被新锚点替换:
  • 如何禁用该功能,所以当我点击第二个锚点时它什么也不做。
  • 现在假设我点击了 ID 为 cancel-edit 的锚点,它会拉回 ID 为 edit1 的锚点并重新激活 foo 函数,所以当我再次点击第二个锚点时,它将再次执行 foo 函数...

我希望它很清楚:X! 提前致谢。

【问题讨论】:

    标签: jquery function bind unbind die


    【解决方案1】:

    如果你想在点击后对所有元素禁用它,你可以设置一个标志:

    var enabled = true;
    function foo(id) {
        if( enabled ) {
            // run your code
            enabled = false;
        }
    }
    

    它会继续运行,但if语句中的代码不会在第一次点击之后。

    function stop() {
    
        // replace the original anchor
    
        enabled = true; // enable the "foo" handler
    
    }
    

    当单击带有stop() 的新锚点时,将其替换为原来的锚点,并将enabled 设置为true,这样foo() 将再次起作用。

    【讨论】:

    猜你喜欢
    • 2013-09-14
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    相关资源
    最近更新 更多