jquery unbind函数事件一旦执行

时间:2011-07-01 02:36:02

标签: jquery function bind unbind die

嗨伙计 请帮我解决这个问题:假设我们从锚点调用相同的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的锚点,它将使用id edit1拉回锚点并重新激活foo函数,这样当我在第二个锚点中重新点击时它将再次执行foo函数... < / LI>

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

1 个答案:

答案 0 :(得分: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()再次有效。

相关问题