从另一个包含的 JavaScript 调用包含的 JavaScript 代码的函数

发布于 2024-08-22 00:09:00 字数 673 浏览 9 评论 0原文

我正在尝试在我的页面中使用不同加载的 JavaScript,但我无法让它们说话。

<html>
<script type="text/javascript" src="jquery144.js"></script>
<script type="text/javascript" src="shared_functions.js"></script>
<script type="text/javascript" src="page_function_callers.js"></script>
</html>


// shared_functions.js
$(document).ready (function () {
    function sayHello (what) {
        alert (what);
    }
});

// page_function_callers.js
$(document).ready (function () {
    $("div.my_button").click (function () {
        sayHello ("Hello world!");
    });
});

我使用 jQuery,我想使用这种方式,因为它应该让我回收许多公共方法。我哪里错了?

I'm trying to work with different loaded JavaScript in my page, but I'm not able to let them talk.

<html>
<script type="text/javascript" src="jquery144.js"></script>
<script type="text/javascript" src="shared_functions.js"></script>
<script type="text/javascript" src="page_function_callers.js"></script>
</html>


// shared_functions.js
$(document).ready (function () {
    function sayHello (what) {
        alert (what);
    }
});

// page_function_callers.js
$(document).ready (function () {
    $("div.my_button").click (function () {
        sayHello ("Hello world!");
    });
});

I work with jQuery and I'd like to use this way because it should let me recyle many public methods. Where I'm wrong?

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

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

发布评论

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

评论(4

淡淡的优雅 2024-08-29 00:09:00

函数 sayHelloDOMReady 事件 ( $(document).ready ) 的 scpoe 中声明。超出该范围将不可用。但是需要在该作用域内声明函数。即使 sayHello 使用了很多 DOM 对象(在你的例子中并没有),它在被调用之前也不会被执行,所以你唯一需要确保的是这样一个直到 DOMReady 后才调用函数。

因此,您可以简单地分别删除shared_functions.js中的第一行和最后一行,即 $(document).ready(function() {}); ,然后您的代码会起作用。

The function sayHello is declared inside the scpoe of the DOMReady event ( $(document).ready ). It will not be available outside of that scope. But there is no need to declare a function inside that scope. Even if sayHello uses a lot of DOM objects (which in your example it doesn't), it will not be executed until it's being called, so the only thing you need to make sure is that such a function is not called until after DOMReady.

So you can simply remove the first and the last line in shared_functions.js, i.e. $(document).ready(function() { and }); respectively, and your code will work.

温暖的光 2024-08-29 00:09:00

当您在“ready”函数中声明“sayHello”(有人记得夏洛特教堂的歌曲吗?天哪,回忆......)时,它是该函数的本地函数。如果您愿意,可以通过这样做将其设置为全局:(

window['sayHello'] = function sayHello(what) {
  alert(what);
};

最好给该函数一个“本地”名称——“function”关键字后面的名称——因为这样该函数就不会在 Firebug 中显示为“匿名”。 )

我鼓励您研究将全局函数放入 jQuery 插件中,或者至少将其放在 jQuery 对象而不是“窗口”上,从而将其放入 jQuery“全局”中。

When you declare "sayHello" (does anybody remember that Charlotte Church song? Gosh, the memories ...) inside the "ready" function, it's local to that function. You can make it global if you like by doing this:

window['sayHello'] = function sayHello(what) {
  alert(what);
};

(It's nice to give the function a "local" name — the name after the "function" keyword — because then the function won't show up as "anonymous" in Firebug.)

I would encourage you to investigate making your global function into a jQuery plugin, or at least a jQuery "global" by putting it on the jQuery object instead of "window".

花桑 2024-08-29 00:09:00

尝试在调用 $(document).ready 之外定义函数 sayHello

Try defining the function sayHello outside of your call $(document).ready.

静赏你的温柔 2024-08-29 00:09:00

在上面的代码中,sayHello() 方法仅在匿名函数的作用域内定义。

此代码可能更适合您:

// shared_functions.js
window.MyNamespace = window.MyNamespace || {};
MyNamespace.sayHello = function(what)
{
  alert(what);    
};

// page_function_callers.js
$(document).ready (function ()
{
    $("div.my_button").click (function ()
    {
      MyNamespace.sayHello('hello world');    
    });
});

请确保您以正确的顺序加载 JS 文件。还有更优雅的解决方案,但这是一个相当简单直接的解决方案。

这种方法的一个优点是不会污染全局名称空间。

In your code above, the sayHello() method is only defined inside the anonymous function's scope.

This code might work better for you:

// shared_functions.js
window.MyNamespace = window.MyNamespace || {};
MyNamespace.sayHello = function(what)
{
  alert(what);    
};

// page_function_callers.js
$(document).ready (function ()
{
    $("div.my_button").click (function ()
    {
      MyNamespace.sayHello('hello world');    
    });
});

Make sure you're loading your JS files in the right order tho. There are more elegant solutions out there, but this is a fairly simple and straightforward one.

One advantage to this approach would be not polluting the global namespace.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文