【问题标题】:Jquery Function HangsJquery 函数挂起
【发布时间】:2013-10-09 20:10:23
【问题描述】:

我正在使用一个名为 h5validate 的插件。我发现启动此插件的唯一方法是单击(模糊)我要验证的字段,因此我的逻辑是设置一个函数,当我单击(碰巧)下一个按钮时为我执行此操作;我试过直接调用 validate 函数,但是没用。

无论如何我的问题是:

1.这个函数让浏览器挂起约 3 秒,为什么?

$(document).on('click', '.tab', function () {
$this = $(this);
    if( $this.is('#tab1') )     { $('.tab-content-first .row .cell input:not(#ImageLocation)').blur(); }
    if( $this.is('#tab2') )     { $('.tab-content-1 .row .cell input:not(#ImageLocation)').blur(); }
    if( $this.is('#tab3') )     { $('.tab-content-2 .row .cell input:not(#ImageLocation)').blur(); }
    if( $this.is('#tab4') )     { $('.tab-content-3 .row .cell input:not(#ImageLocation)').blur(); }
    if( $this.is('#tab5') )     { $('.tab-content-4 .row .cell input:not(#ImageLocation)').blur(); }
    if( $this.is('#tab6') )     { $('.tab-content-5 .row .cell input:not(#ImageLocation)').blur(); }
});

编辑: 我已经兑现了 $('this');作为建议的对象。

我已经确定它可能会冒泡,文档建议我 stoppropagation(),但这不起作用,我正在使用委托,因为我通过 ajax 调用它可能这就是 stoppropagation() 没有的原因工作吗?

【问题讨论】:

    标签: javascript jquery event-bubbling event-delegation


    【解决方案1】:

    首先:$(this) 应该被缓存。 $() 的其余部分也可以被缓存,并且在函数之前。

    第二:使用策略模式

    【讨论】:

    • 有趣,我先给它。
    • 如果仍有问题,请提供更多代码或应用链接,因为我无能为力:)
    • 所以不应该自己锁定?
    • 添加更多代码,因为这并没有告诉我们太多关于冒泡的信息。如果您可以将其复制到具有相同行为(3s 挂起)的 jsfiddle 或 codepen,那就太好了。
    猜你喜欢
    • 1970-01-01
    • 2020-01-08
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2021-04-09
    • 2021-06-02
    相关资源
    最近更新 更多