如何在$(文档)中调用函数。准备从外面

时间:2022-04-24 07:27:24

How do you call function lol() from outside the $(document).ready() for example:

如何从$(文档)之外调用函数lol()。ready(),例如:

$(document).ready(function(){  
  function lol(){  
    alert('lol');  
  }  
});  

Tried:

尝试:

$(document).ready(function(){
  lol();
});

And simply:

和简单的:

lol();

It must be called within an outside javascript like:

它必须在javascript外部调用,比如:

function dostuff(url){
  lol(); // call the function lol() thats inside the $(document).ready()
}

5 个解决方案

#1


50  

Define the function on the window object to make it global from within another function scope:

定义窗口对象上的函数,使其在另一个函数范围内成为全局的:

$(document).ready(function(){  
  window.lol = function(){  
    alert('lol');  
  }  
});

#2


37  

Outside of the block that function is defined in, it is out of scope and you won't be able to call it.

在函数被定义的块之外,它超出了范围,你不能调用它。

There is however no need to define the function there. Why not simply:

但是,不需要在那里定义函数。为什么不简单:

function lol() {
  alert("lol");
}

$(function() {
  lol(); //works
});

function dostuff(url) {
  lol(); // also works
}

You could define the function globally like this:

你可以这样全局地定义函数:

$(function() {
  lol = function() {
     alert("lol");
  };
});
$(function() {
  lol();
});

That works but not recommended. If you're going to define something in the global namespace you should use the first method.

这是可行的,但不推荐。如果要在全局命名空间中定义某些东西,应该使用第一个方法。

#3


3  

Short version: you can't, it's out of scope. Define your method like this so it's available:

短版本:你不能,它超出了范围。像这样定义你的方法,使其可用:

function lol(){ 
  alert('lol'); 
} 

$(function(){
  lol();
});

#4


3  

You don't need and of that - If a function is defined outside of Document.Ready - but you want to call in it Document.Ready - this is how you do it - these answer led me in the wrong direction, don't type function again, just the name of the function.

如果函数是在文档之外定义的,则不需要这样做。准备好了——但是您需要调用it文档。准备好了,这就是你怎么做的这些答案把我引向了错误的方向,不要再输入函数,只输入函数名。

      $(document).ready(function () {
     fnGetContent();
      });

Where fnGetContent is here:

fnGetContent在哪里:

       function fnGetContent(keyword) {
            var NewKeyword = keyword.tag;
            var type = keyword.type;
            $.ajax({ .......

#5


0  

What about the case where Prototype is installed with jQuery and we have noconflicts set for jQuery?

如果使用jQuery安装Prototype并且没有为jQuery设置冲突,该怎么办?

jQuery(document).ready(function($){  
     window.lol = function(){  
          $.('#funnyThat').html("LOL");
     }  
});

Now we can call lol from anywhere but did we introduce a conflict with Prototype?

现在我们可以从任何地方调用lol但是我们引入了与Prototype的冲突吗?

#1


50  

Define the function on the window object to make it global from within another function scope:

定义窗口对象上的函数,使其在另一个函数范围内成为全局的:

$(document).ready(function(){  
  window.lol = function(){  
    alert('lol');  
  }  
});

#2


37  

Outside of the block that function is defined in, it is out of scope and you won't be able to call it.

在函数被定义的块之外,它超出了范围,你不能调用它。

There is however no need to define the function there. Why not simply:

但是,不需要在那里定义函数。为什么不简单:

function lol() {
  alert("lol");
}

$(function() {
  lol(); //works
});

function dostuff(url) {
  lol(); // also works
}

You could define the function globally like this:

你可以这样全局地定义函数:

$(function() {
  lol = function() {
     alert("lol");
  };
});
$(function() {
  lol();
});

That works but not recommended. If you're going to define something in the global namespace you should use the first method.

这是可行的,但不推荐。如果要在全局命名空间中定义某些东西,应该使用第一个方法。

#3


3  

Short version: you can't, it's out of scope. Define your method like this so it's available:

短版本:你不能,它超出了范围。像这样定义你的方法,使其可用:

function lol(){ 
  alert('lol'); 
} 

$(function(){
  lol();
});

#4


3  

You don't need and of that - If a function is defined outside of Document.Ready - but you want to call in it Document.Ready - this is how you do it - these answer led me in the wrong direction, don't type function again, just the name of the function.

如果函数是在文档之外定义的,则不需要这样做。准备好了——但是您需要调用it文档。准备好了,这就是你怎么做的这些答案把我引向了错误的方向,不要再输入函数,只输入函数名。

      $(document).ready(function () {
     fnGetContent();
      });

Where fnGetContent is here:

fnGetContent在哪里:

       function fnGetContent(keyword) {
            var NewKeyword = keyword.tag;
            var type = keyword.type;
            $.ajax({ .......

#5


0  

What about the case where Prototype is installed with jQuery and we have noconflicts set for jQuery?

如果使用jQuery安装Prototype并且没有为jQuery设置冲突,该怎么办?

jQuery(document).ready(function($){  
     window.lol = function(){  
          $.('#funnyThat').html("LOL");
     }  
});

Now we can call lol from anywhere but did we introduce a conflict with Prototype?

现在我们可以从任何地方调用lol但是我们引入了与Prototype的冲突吗?