javascript 从 firebug 控制台调用自定义函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9354191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:22:40  来源:igfitidea点击:

Calling custom functions from firebug console

javascriptjqueryfunctionconsolefirebug

提问by E.E.33

I created a document that loads a .js document that has some JQuery functions that I have created. I would like to use the firebug console to quickly test the functionality of these functions on my html document. But when I try and call these function in the console I don't get any response.

我创建了一个加载 .js 文档的文档,该文档包含我创建的一些 JQuery 函数。我想使用 firebug 控制台在我的 html 文档上快速测试这些函数的功能。但是当我尝试在控制台中调用这些函数时,我没有得到任何响应。

for example:

例如:

  • I have index.html that call my JS:

    <script src="jquery.js" type"text/javascript"></script>
    <script src="myfunctions.js" type="text/javascript"></script>
    
  • Myfuntions.js has the following defined in it:

    function showAbout(){
      $('div#about').show("slow");
      $('.navbar #about-button').attr('disabled', true); 
    }
    
  • 我有调用我的 JS 的 index.html:

    <script src="jquery.js" type"text/javascript"></script>
    <script src="myfunctions.js" type="text/javascript"></script>
    
  • Myfuntions.js 中定义了以下内容:

    function showAbout(){
      $('div#about').show("slow");
      $('.navbar #about-button').attr('disabled', true); 
    }
    

The Problem:

问题:

When i try to call showAboutor showAbout()from the console on index.html I don't get any changes. However, when I call $('div#about').show("slow");or $('div#about').show("slow");from the console directly I get the expected behavior.
What is the proper way to call a user defined function from the console?

当我尝试 在 index.html 上调用showAboutshowAbout()从控制台调用时,我没有得到任何更改。但是,当我直接从控制台调用$('div#about').show("slow");或调用时,我$('div#about').show("slow");得到了预期的行为。
从控制台调用用户定义函数的正确方法是什么?

回答by T.J. Crowder

If showAboutis defined at the global scope, you should be able to write showAbout();in the console and see the result. If not, then you're probably putting your functions in a scoping functionlike so:

如果showAbout在全局范围内定义,您应该可以showAbout();在控制台中写入并查看结果。如果没有,那么您可能会将您的函数放在一个范围函数中,如下所示:

(function() {
    function showAbout() {
    }
})();

If so, good for you, you've avoided created global variables/functions. But it does mean you can't access those functions from the console, because the console only has access to global scope.

如果是这样,那对您有好处,您已经避免了创建全局变量/函数。但这确实意味着您无法从控制台访问这些功能,因为控制台只能访问全局范围。

If you want to export any of those so you can use them from the console (perhaps only temporarily, for debugging), you can do that like this:

如果您想导出其中任何一个以便您可以从控制台使用它们(可能只是暂时的,用于调试),您可以这样做:

(function() {
    window.showAbout = showAbout;
    function showAbout() {
    }
})();

That explicitly puts a showAboutproperty on the global object (window) referencing your function. Then showAbout();in the console will work.

这显式地将一个showAbout属性放在window引用您的函数的全局对象 ( ) 上。然后showAbout();在控制台中将工作。

回答by alex

Your showAbout()probably isn't in the global scope.

showAbout()可能不在全球范围内。

It may be wrapped by a $(document).ready()code, and its scope limited to that function.

它可能被$(document).ready()代码包裹,其范围仅限于该功能。

For testing, you could add beneath...

为了测试,您可以在下面添加...

window.showAbout = showAbout;

You can then call showAbout()from your console, once the function has been defined.

showAbout()一旦定义了函数,您就可以从控制台调用。