如何在 jQuery 中跳转或逃逸链

发布于 2024-12-08 09:05:39 字数 376 浏览 3 评论 0原文

如何在 jQuery 中跳跃或逃逸链。

例如。

$("h3").click(function(){
    //doSomething
    if(~~)  
        // in this case, escape chain(A and B function will be not work)
    else if(~~) 
        // in this case, jump to B case(A function will be not work)
})
.bind(A, function(){
    do A case.
})
.bind(B, function(){
    do B case.
});

是否可以?

How to jumping or escape chain in jQuery.

for example.

$("h3").click(function(){
    //doSomething
    if(~~)  
        // in this case, escape chain(A and B function will be not work)
    else if(~~) 
        // in this case, jump to B case(A function will be not work)
})
.bind(A, function(){
    do A case.
})
.bind(B, function(){
    do B case.
});

is it possible?

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

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

发布评论

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

评论(3

后知后觉 2024-12-15 09:05:39

单击处理程序中的代码在单击实际发生之前不会执行,而绑定调用是在应用单击处理程序后立即处理的。看起来您想要的是各种处理程序的条件执行。您可以通过在原始单击处理程序中设置元素上的数据,然后检查后续处理程序中的状态来实现此目的,但最好将它们创建为独立函数,然后根据需要从单击处理程序中调用它们。

The code in the click handler isn't executed until the click actually happens, while the bind calls are handled right after the click handler is applied. It looks like what you want is conditional execution of the various handlers. You could achieve that by setting data on the element in the original click handler, then checking the state in the subsequent handlers, but it would probably be better to create them as standalone functions and simply call them from the single click handler as appropriate.

无声静候 2024-12-15 09:05:39

如果 A 和 B 实际上只是函数,其中之一应该在单击时调用,请执行以下操作:

function A()
{
  ...
}
function B()
{
  ...
}

$("h3").click(function(){
    //doSomething
    if(~~)  
        A();
    else if(~~) 
        B();
})

但是,您的问题并不完全清楚。

If A and B are actually just functions, one of which should be called on click, do:

function A()
{
  ...
}
function B()
{
  ...
}

$("h3").click(function(){
    //doSomething
    if(~~)  
        A();
    else if(~~) 
        B();
})

However, your question is not completely clear.

究竟谁懂我的在乎 2024-12-15 09:05:39

要结束你的链,你可以使用 jQuery .end() 方法;但在这种情况下,您有一个条件,并且应该为每个条件运行两个不同的代码。因此,您可以在您的条件下使用 jQuery $(this) 来引用单击的内容并根据您的条件运行代码:

$("h3").click(function(){
    //doSomething
    if(~~)  
        // in this case, escape chain(A and B function will be not work)
        $(this).bind(A, function(){
            //do A case.
         })
    else if(~~) 
        // in this case, jump to B case(A function will be not work)
       $(this).bind(B, function(){
          //do B case.
       });
});

To end your chain you can use jQuery .end() method; But in this case you have a condition and two different code should run for each conditions. So you can use jQuery $(this) under your condition to refer to what is clicked and run the code based on your condition:

$("h3").click(function(){
    //doSomething
    if(~~)  
        // in this case, escape chain(A and B function will be not work)
        $(this).bind(A, function(){
            //do A case.
         })
    else if(~~) 
        // in this case, jump to B case(A function will be not work)
       $(this).bind(B, function(){
          //do B case.
       });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文