Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery函数卡在循环中_Javascript_Jquery_Html - Fatal编程技术网

Javascript jQuery函数卡在循环中

Javascript jQuery函数卡在循环中,javascript,jquery,html,Javascript,Jquery,Html,我有一个简单的div,其中包含以下链接: <div class="mhButton"> <a href="#" id="RegisterAnimal"><span class="icon-checkmark"></span>&nbsp;&nbsp;Register Animal</a> </div> 这是可行的,但是,我陷入了一个循环,运行了639次 我不明白为什么它会运行X次,然后继续运行而不会出

我有一个简单的div,其中包含以下链接:

<div class="mhButton">
    <a href="#" id="RegisterAnimal"><span class="icon-checkmark"></span>&nbsp;&nbsp;Register Animal</a>
</div>
这是可行的,但是,我陷入了一个循环,运行了639次

我不明白为什么它会运行X次,然后继续运行而不会出错

有人有办法防止这种情况发生吗?并解释为什么会发生这种情况

注意*控制台一次又一次地记录相同的按钮。

当您调用
$(this)时。查找(“a”)。单击()
事件将冒泡到
div.mhButton
标记,并导致再次调用处理程序。它运行大约500次的原因是因为它在堆栈溢出时停止,而不会继续

您可以通过检查单击是否为

关于已接受答案的注释

Bic的第二个答案几乎有效,我的是一种副作用较少的不同方法。调用
stopPropagation
的主要问题是,在这种情况下,整个文档上可能存在不会被触发的处理程序。commmon case是指当您单击页面上的任何其他位置时,菜单或对话框本应隐藏。
stopPropagation
方法将防止用户单击按钮时菜单被隐藏

$(".mhButton").click(function (e) {       
    // Only run this handler if the click was on the div, not the link itself 
    if ( $(e.target).is('a, a *') ) {
        return;
    }
    var a = $(this).find("a").text();
    $(this).find("a").click();
});

由于按钮中嵌入了
a
标记,因此您将持续重新触发事件。事件将冒泡,因此将单击锚点,然后单击其父锚点。它一直在运行,直到浏览器厌倦了它,然后它就停止了。该方法实际上没有做任何事情,这可能是您没有看到任何问题的原因。您可以通过以下两种方式实现目标:

$(".mhButton").click(function () {
    $(this).off('click'); // turn the click handler off in the handler itself. 
    var a = $(this).find("a").text();
    console.log(a);
    $(this).find("a").click();
});
如果这样做,那么最终只能触发一次事件

或者:

$(".mhButton").click(function (e) {
    a = $(this).find("a").text();
    console.log(a);
    $(this).find("a").click();
});

$("#RegisterAnimal").click(function (e) {
    e.stopPropagation(); // prevent the anchor from re-firing the button click
});

或者,您可以将链接设置为按钮样式,避免不必要的单击处理程序。

为什么要执行
.off()。click()
?正在尝试从单击中删除事件处理程序,并用正确的代码更新了Question。感谢您指出这一点。可能是因为您在单击容器时触发了锚点的单击,然后在单击锚点时触发了容器的单击,依此类推,所以它会变成圆形。为什么不将容器绑定到锚处理程序并执行StopproPagation在函数中添加ol'
e.preventDefault()
时会发生什么情况?@ckpepper02 preventDefault会阻止默认操作,即跟踪链接,这不会有帮助,但如何阻止它呢?我需要单击它。@标记为什么需要单击它?您不能将
window.location.href
设置为所需的URL吗?因为它实际上触发了一个ajax调用。请参阅我的更新,调用
click()
会导致处理程序再次被调用,这就是我需要的。@JuanMendes…这正是它在代码后面所说的:p@Mark您的方法唯一有效的问题是,文档上可能有一个永远不会触发的单击事件。当您单击页面上的其他位置时,菜单和弹出窗口通常会被取消。使用
stopPropagation
方法不会隐藏菜单。
$(".mhButton").click(function (e) {
    a = $(this).find("a").text();
    console.log(a);
    $(this).find("a").click();
});

$("#RegisterAnimal").click(function (e) {
    e.stopPropagation(); // prevent the anchor from re-firing the button click
});