gpt4 book ai didi

javascript - 多个页面的一个 JS 文件

转载 作者:IT王子 更新时间:2023-10-29 03:03:44 26 4
gpt4 key购买 nike

<分区>

我通常将所有 JavaScript 脚本放入一个文件中,例如scripts.js(HTTP 请求越少越好)。因此,正如预期的那样,有些页面需要一些脚本,有些则不需要。

要定位到特定页面,我使用类似的东西:

if ($("body#share").length > 0) {
// Place the logic pertaining to the page with ID 'share' here...
}

// The file / script continues...

其他或更好的建议?谢谢!

澄清:不是寻找将多个 JS 文件合并成一个大文件和保留多个单独的 JS 文件之间的利弊。答案肯定是“视情况而定”(我们知道)。我的问题是,假设我所有的 JS 逻辑都放在一个大文件中,如何让特定的(大块的)脚本在加载相应页面时运行?我曾经使用的一种方法是使用 if ($('#id-of-target-element')) {/* run the script */>;有没有更好的办法?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com