jquery中的递归错误太多

发布于 2024-09-28 20:27:46 字数 518 浏览 5 评论 0原文

这段代码:

$(文档).ready(function() {
$('body').click(function(evt) {
if(evt.target.nodeName === 'A' && $(evt.target).hasClass('交叉链接')) {
$('a[href=#2]').trigger('点击');
} });});

给我一个“太多递归”的错误,

人们可能会认为我应该将一个处理程序附加到交联元素上。我尝试了这个,但我无法让它工作,因为 DOM 在创建交叉链接类元素之前加载。我需要做什么来解决这个问题,或者您是否更好地了解我应该做什么来实现我正在尝试做的事情?

如果您想亲自查看错误,请访问 eataustineat.com/testfolder/ 在搜索字段中输入“d” 选择狗全能(这是你应该注意到“太多递归错误”的地方) 它会将 div 向左移动,但这样做会非常错误。

this code:

$(document).ready(function() {
$('body').click(function(evt) {
if(evt.target.nodeName === 'A' && $(evt.target).hasClass('cross-link'))
{
$('a[href=#2]').trigger('click');
} });});

given me and error of "too much recursion"

one might think that I should just attach a handler to the crosslink element. i tried this, but I couldn't get it to work because the DOM loads before the cross-link class elements are created. what do I need to do to fix this or do you have a better idea of what I should do to implement what I'm trying to do?

if you want to see the error for yourself, do to eataustineat.com/testfolder/
type in a 'd' in the search field
select dog almighty (this is where you should notice the "too much recursion error"
it will move the div to the left, but it will do so very buggily.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

悟红尘 2024-10-05 20:27:46

您可以使用 livedelegate 为稍后创建的元素添加监听器:

$("a.cross-link").live("click", function()
{
   $('a[href=#2]').trigger('click');
   window.location.hash = "#2";
});

但是,单击不会触发转到 URL 的默认事件,因此您需要执行以下操作手动那个。

You can use live or delegate to add listeners for elements that are created later:

$("a.cross-link").live("click", function()
{
   $('a[href=#2]').trigger('click');
   window.location.hash = "#2";
});

However, click does not trigger the default event of going to the URL, so you need to do that manually.

溺孤伤于心 2024-10-05 20:27:46

如果在创建文档后添加需要现有事件的元素,您可以使用 live

$(document).ready(function() {
    $('.cross-link').live(function() {
        $('a[href=#2']).click(); //No cross-link class allowed on this element as it is responsible for the recursion
    });
});

If elements that need an existing event are added after document creation you can use live

$(document).ready(function() {
    $('.cross-link').live(function() {
        $('a[href=#2']).click(); //No cross-link class allowed on this element as it is responsible for the recursion
    });
});
花开浅夏 2024-10-05 20:27:46

好吧,递归来自触发

$('a[href=#2]').trigger('click');

当从事件中单击此元素时,它会抛出另一个事件,该事件将由相同的代码处理,依此类推。

这应该可行:

    $(document.ready)(function(){
      $('.cross-link').click(function(){
       #('a[href=#2').click();
});
});

另外,从性能角度来看,向第二个链接添加 id 更为理想,因为通过 id 选择比通过属性选择更快。如果您仍然想通过 href 选择并且只有一个这样的链接,请执行以下操作:

#('a[href=#2 :first').click();

Well the recursion comes from triggering

$('a[href=#2]').trigger('click');

When this element is clicked from the event it throw yet another event which will be handled by the same code and so on.

This should work:

    $(document.ready)(function(){
      $('.cross-link').click(function(){
       #('a[href=#2').click();
});
});

Also performance-wise it is more optimal to add an id to your second link because selecting by an id is faster than selecting by an attribute. If you still want to go with selecting by href and there is only one such link do:

#('a[href=#2 :first').click();
风苍溪 2024-10-05 20:27:46

参考我上面的评论,这就是我的做法。我建议使用live...


var open = function (myObj, animationTime) {
    //do stuff
    $(myObj).unbind('click');
    $(myObj).click(function () {
        close(myObj, timer);
    });
}

var close = function (myObj, animationTime) {
    //dostuff
    //remove close click event and then rebind the click event to open
    $(myObj).unbind('click');
    $(myObj).click(function () {
        open(myObj, timer);
    });
}

$(".mySelector").click(function () {
    open($(this), timer);
});


for referance to my comment above this is how i did it. I recommend using live tho...


var open = function (myObj, animationTime) {
    //do stuff
    $(myObj).unbind('click');
    $(myObj).click(function () {
        close(myObj, timer);
    });
}

var close = function (myObj, animationTime) {
    //dostuff
    //remove close click event and then rebind the click event to open
    $(myObj).unbind('click');
    $(myObj).click(function () {
        open(myObj, timer);
    });
}

$(".mySelector").click(function () {
    open($(this), timer);
});


〃安静 2024-10-05 20:27:46
$('body').click(function(evt) {
    if(evt.target.nodeName === 'A' && $(evt.target).hasClass('cross-link')) {
        $('a[href=#2]').trigger('click');
    }
  });

您错过了 if-else 语句的 else

$('body').click(function(evt) {
    if(evt.target.nodeName === 'A' && $(evt.target).hasClass('cross-link')) {
        $('a[href=#2]').trigger('click');
    }
  });

You miss the else of if-else statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文