gpt4 book ai didi

javascript - 将 block JavaScript 代码映射为仅在特定页面上运行

转载 作者:行者123 更新时间:2023-12-02 23:40:32 24 4
gpt4 key购买 nike

我目前正在使用 webpack 将所有 .js 捆绑到一个 app.js 中,该 app.js 在我项目的所有页面中使用。然而,我遇到了一个问题,一个页面的代码干扰了另一个页面(不需要的地方)!如果存在相同的选择器,则选择器上的事件监听器将在其他页面上触发!

当在整个应用程序中使用一个 JavaScript 文件时,如何在特定页面上运行应用程序上的特定代码?

最佳答案

您可以使用window.location.pathname来获取页面的url

var url = window.location.pathname;
var filename = url.substring(url.lastIndexOf('/')+1);
if (filename == 'thepageyouwant') [
// run your javascript
}

关于javascript - 将 block JavaScript 代码映射为仅在特定页面上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56099251/

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