【问题标题】:Load one time beside the $(document).ready(...) ? Jquery在 $(document).ready(...) 旁边加载一次? jQuery
【发布时间】:2013-03-07 17:00:14
【问题描述】:

我在触发一个只需要加载一次的函数时遇到问题。但我不想把它放进去:

jQuery(document).ready(function ($) {

});

我想单独运行它。

我试过了:

 jQuery(document).one(function () {
        myfunction();
    });

还有一些其他的东西。 但无法解决。

更新:

我的网络服务已准备就绪():

jQuery(document).ready(function ($) {
    PageMethods.myWebSer(suc, fail);
});

function suc(){
//I want to run my function here , but only one time
//Examplle
 //jQuery(document).one(function () {
          //  myfunction();
       // });
}

谢谢

【问题讨论】:

  • 你想在哪里'put'呢? IE。它应该什么时候运行,时机明智? onreadyonload无论何时
  • 其实它是在外面,它会在另一个函数上运行。
  • 分开是什么意思?您希望何时调用此函数?
  • 我要更新我的问题
  • 你想只运行一次myfunction,还是只运行一次suc

标签: javascript jquery


【解决方案1】:

只需添加另一个 readyload 函数:您可以拥有任意数量的函数,它们都会按顺序调用:

jQuery(document).ready(function() {
   // this will be run
});
jQuery(document).ready(function() {
   // and this one too (after the other one)
});

【讨论】:

  • 一定要强调ready 事件只会触发一次。 OP 似乎不明白这一点。
【解决方案2】:

如果你想让它运行onload 使用:

jQuery(window).load(function () {
    // run
});

请记住,ready 会在 load 之前触发。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多