Javascript 如何在jquery中重新绑定事件?

Javascript 如何在jquery中重新绑定事件?,javascript,jquery,onclick,Javascript,Jquery,Onclick,我有以下JavaScript/jQuery代码,用于启动突出显示DOM元素的侦听器 我单击一个按钮并启动侦听器事件。ex:突出显示:函数() 当我单击网页中的某个内容时,我会停止侦听器 现在,当我再次单击按钮时,我想重新启动侦听器事件 highlight : function() { if(isHighlighting){ isHighlighting = false; $(document).unbind("mousemo

我有以下JavaScript/jQuery代码,用于启动突出显示DOM元素的侦听器

我单击一个按钮并启动侦听器事件。ex:突出显示:函数()

当我单击网页中的某个内容时,我会停止侦听器

现在,当我再次单击按钮时,我想重新启动侦听器事件

highlight : function()
    {
        if(isHighlighting){
            isHighlighting = false;
            $(document).unbind("mousemove", highlighter);
            return false;
        }

        $(document).bind("mousemove", highlighter);
        isHighlighting = true;
    },
我还有捕获onclick事件并停止DOM元素highlighter的代码

function highlighter(e) {
    var cur, overlay = $("#overlayhighlightclosetaffair"),
    no = [document.body, document.documentElement, document];
    if (e.target === cur) {
        return;
    }

    if (~no.indexOf(e.target)) {
        cur = null;
        overlay.hide();
        return;
    }

    var target = $(e.target),
    offset = target.offset(),
    width = target.outerWidth(),
    height = target.outerHeight();
    cur = e.target;
    overlay.css({
        top: offset.top,
        left: offset.left,
        width: width,
        height: height
    }).show();

    $(document).click(
        function() {
            if(isHighlighting){
                isHighlighting = false;
                $(document).unbind("mousemove", highlighter);
                console.log('click');
            }
    });
}

您不需要解除事件绑定,并且可以简化逻辑

这不是一个功能完整的示例,但它应该是一个很好的起点。我建议对代码进行结构化,使
isHighlighting
不是全局的

function highlightElement(e) {
    if(isHighlighting) {
        /* this is where you put the code to highlight e.target */
    }
}

/* call this one time */
$(document).click(
    function(e) {
        /* this will be true if we clicked the button, false if we clicked anything else */
        isHighlighting = e.target === highlightButtonNode;
    }
});

/* also call this once */
$(document).bind("mousemove", highlightElement);
这里有一个使用绑定和解除绑定的替代解决方案。我不推荐这种方法,但这是处理解绑和重新绑定的更好方法。忘记设置
isHighlight=false
。是一个比忘记解开事件绑定要轻得多的bug。这将导致内存泄漏,对事件处理程序的多次调用更有可能产生更严重的副作用

/* call this one time */
$(document).click(
    function(e) {
        /* this will be true if we clicked the button, false if we clicked anything else */
        if(e.target === highlightButtonNode) {
            $(document).bind("mousemove", highlightElement);
        } else {
            $(document).unbind("mousemove", highlightElement);
        }
    }
});

您不需要解除事件绑定,并且可以简化逻辑

这不是一个功能完整的示例,但它应该是一个很好的起点。我建议对代码进行结构化,使
isHighlighting
不是全局的

function highlightElement(e) {
    if(isHighlighting) {
        /* this is where you put the code to highlight e.target */
    }
}

/* call this one time */
$(document).click(
    function(e) {
        /* this will be true if we clicked the button, false if we clicked anything else */
        isHighlighting = e.target === highlightButtonNode;
    }
});

/* also call this once */
$(document).bind("mousemove", highlightElement);
这里有一个使用绑定和解除绑定的替代解决方案。我不推荐这种方法,但这是处理解绑和重新绑定的更好方法。忘记设置
isHighlight=false
。是一个比忘记解开事件绑定要轻得多的bug。这将导致内存泄漏,对事件处理程序的多次调用更有可能产生更严重的副作用

/* call this one time */
$(document).click(
    function(e) {
        /* this will be true if we clicked the button, false if we clicked anything else */
        if(e.target === highlightButtonNode) {
            $(document).bind("mousemove", highlightElement);
        } else {
            $(document).unbind("mousemove", highlightElement);
        }
    }
});

我认为你需要重新措辞或澄清,因为你似乎理解如何绑定/取消绑定,但不清楚你需要什么。我理解如何绑定/取消绑定,但问题是:如果我绑定,那么取消绑定,为什么我可以再次绑定?这是错别字吗?:
if(~no.indexOf(e.target)){
我认为您需要重新措辞或澄清,因为您似乎理解如何绑定/解除绑定,但不清楚您需要什么。我理解如何绑定/解除绑定,但问题是:如果我绑定,那么解除绑定,为什么我可以再次绑定?这是一个拼写错误吗?:
如果(~no.indexOf(e.target)){
我不明白一些事情。我有两个函数,第一个高亮显示,第二个高亮显示;从第一个我调用第二个,在第二个我有一个单击事件的侦听器;在document.click上,isHighlighting始终为true,因为我将其设置为true,这是我解除绑定事件的地方;如果我解除绑定,我如何重新绑定?我可以使用live解决这个问题吗代替绑定?@IonutFlaviusPogacian,我不理解你的评论或你用来解决问题的方法。你使用的是某种毫无意义的循环逻辑。你误解了我回答的要点,你只需要绑定事件一次,而不需要解除绑定。我添加了一个更完整的示例。即使呃,我建议不要这样做,我还添加了一个更好的重新绑定方法的示例。我不明白一些事情。我有两个函数,第一个高亮显示,第二个高亮显示;从第一个我调用第二个,在第二个我有一个单击事件的侦听器;在document.click上,高亮显示总是正确的,因为我将它设置为正确的,这就是我要做的地方nbind事件;如果我解开,我如何重新绑定?我可以用live而不是bind来解决这个问题吗?@IonutFlaviusPogacian,我不理解你的评论,也不理解你解决问题的方法。你使用的是某种毫无意义的循环逻辑。你误解了我回答的要点,你只需要绑定eve我已经添加了一个更完整的示例。尽管我建议不要使用它,但我也添加了一个更好的重新绑定方法的示例。