有时加载 JavaScript

发布于 2024-10-07 18:56:25 字数 850 浏览 6 评论 0原文

我想在给定页面上有条件地加载一组 javascript 函数(涉及 jQuery)。

情况是,我们的网站有很多发生在 $(document).ready 上的内容(主要是精美的菜单设置和一些 CSS 类操作),但是一两个页面需要更多设置。这是足够的代码(而且足够具体),我不想只是将其扔到主文件中,而是只是将其加载到那些特定的页面上。

看来我不能通过仅将新文件 specic.js 加载到包含

(function () {
   $(something).click(function () { Stuff(happens); });
   something(Else);
   ...
 } ());

在上面的示例中的那些页面中来做到这一点, something(Else); 工作正常,但 .click.bind 似乎没有做任何事情。我最终要做的是

function specificStuff () {
   $(something).click(function () { Stuff(happens); });
   something(Else);
   ...
};

if(specialStuff) SpecificStuff(); 添加到主 js 文件中。它有效,但似乎应该有一种更好的方法来实现这一点(理想的情况是,将所有更改保留在 special.js 中,而不触及常规设置)。

是否有条件加载 js 代码以在 document.ready 中运行的规范方法?

I'd like to conditionally load a set of javascript functions (which involve jQuery) on a given page.

The situation is that our site has a bunch of stuff that happens on $(document).ready (mostly fancy menu setup and a couple of CSS class manipulations), but one or two pages need more setup. It's enough code (and specific enough) that I don't want to just toss it into the main file, but rather just load it on those specific pages.

It seems that I can't do this by just loading a new file specific.js into those pages that contains

(function () {
   $(something).click(function () { Stuff(happens); });
   something(Else);
   ...
 } ());

In the above example, something(Else); works fine, but .click and .bind don't seem to do anything. What I ended up doing is

function specificStuff () {
   $(something).click(function () { Stuff(happens); });
   something(Else);
   ...
};

and adding if(specificStuff) specificStuff(); to the main js file. It works, but it seems like there should be a better way of accomplishing this (ideally one that would keep all the changes in specific.js and not touch the general settings).

Is there a canonical way of conditionally loading js code to run in document.ready?

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

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

发布评论

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

评论(4

笑,眼淚并存 2024-10-14 18:56:25

您可以在网页/脚本文件中多次调用 $(document).ready(); 。只需将 jquery 绑定包装在 special.js 文件中即可:

$(document).ready(function(){
$(something).click(function () { Stuff(发生); });
某事(其他);});

You can call $(document).ready(); multiple times in a web page/script file. Just wrap your jquery bindings as such in your specific.js file:

$(document).ready(function(){
$(something).click(function () { Stuff(happens); });
something(Else);});

如若梦似彩虹 2024-10-14 18:56:25

您可以在这些页面的 html 中加载页面特定的 Javascript,每个脚本都有自己的 document.ready 函数。

You can load the page specific Javascript in the html of those pages, each script with its own document.ready function.

满意归宿 2024-10-14 18:56:25

尝试在将函数传递给 document.ready 时删除 ():

(function () {
   $(something).click(function () { Stuff(happens); });
   something(Else);
   ...
 } **()**);

使用 (),它将立即执行,而不是等待文档准备好。

try removing the () when passing a function to document.ready:

(function () {
   $(something).click(function () { Stuff(happens); });
   something(Else);
   ...
 } **()**);

with the (), it will execute right away and not wait for the document to be ready.

萌辣 2024-10-14 18:56:25

您可以根据需要多次调用 jquery 文档就绪函数。我认为你的问题在于你如何设置你的功能。如果您尝试调用就绪函数,则应如下所示:

$(function () {
   $(something).click(function () { Stuff(happens); });
   something(Else);
   ...
});

此外,如果 something 元素是由 main.js 文件创建的,则应将其包含在 之前 您的特定.js 文件。

You can call the jquery document ready function as many times as you need. I think your issue is with how you've set up your function. If you're trying to call the ready function, it should be like this:

$(function () {
   $(something).click(function () { Stuff(happens); });
   something(Else);
   ...
});

Also, if the something elements are created by your main.js file, you should include it before your specific.js file.

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