【问题标题】:Reloading of the js scripts via jquery load /ajax通过 jquery load /ajax 重新加载 js 脚本
【发布时间】:2014-06-11 09:53:57
【问题描述】:

你好我正在做一个项目。项目的设计是这样的

<html>
<head>
some intail scripts that are needed like bootstrap and jquery  and page specific scripts goes here that is required for the first launch
</head>
<body>
<div id=menu>
<!--we put bootstrap menu here-->
</div>
<div id=container>
<!--we load different html pages here using ajax call inside the div when user clicks on the menu we call page html using ajax and append  it here -->
</div>
<div id=footer>
<!--Footer contents  come here-->
</div>

</body>

我面临的问题是,假设我设计了一个需要引导程序和 jquery 的页面

所以我将文件放在页面中,但这些文件也存在于主页中,我将使用 ajax 加载内容。所以我们最终在页面上有两次 jquery 和引导程序,这导致很少问题 喜欢我的菜单停止工作

如果我从页面中删除引导程序,我将加载页面的一些功能停止工作 bootstrap modals 没有显示并说未定义的功能,但 bootstrap 出现在我正在使用 ajax 加载此页面 html 的主页中。

我确信这是一个项目设计缺陷,但这个项目处于我们无法改变结构的水平。

所以我需要帮助,我应该如何停止重新加载脚本并保持一切正常。

还有一点,主页面是index.html,后端是spring框架

【问题讨论】:

  • 如果我正确理解了您的问题,您应该在需要的每个 HTML 页面中加载 JS 和 CSS 文件。
  • @pilot 是的,但是当我在每个上都包含 js 和 css 时,问题是 js 可能已经像 jquery 一样存在于 index.html 中,所以当我使用 ajax 调用页面时会有 2 jQuery 加载

标签: java javascript jquery html spring


【解决方案1】:

我可以看到两种可能的解决方案:

  1. 如果您要将完整的 HTML 页面放入 &lt;div id=container&gt;,请将它们放入 iframe
  2. 从您导入的页面中去除 HTML 标头,并创建一个适当的主干视图来显示此内容。

【讨论】:

    【解决方案2】:

    您确定在该 div 上使用简单的 JQuery $load() 不会获得主页的包含吗?

    我总是使用 load 将 HTML 代码添加到我的 DOM 中,我不需要为每个注入的页面包含 js 或 css。

        $('#container').load("url_controller_where_you_will_render_the_html_to_inject");
    

    【讨论】:

    • 脚本包括但如果我在页面上添加 jquery 并且 jquery 也出现在主页上,所以当我使用加载方法调用页面时,我们已经加载了两个 jquery,这使得某些功能不好好工作。这就是问题
    • 好的,但我要说的是,如果主页上已经有 jQuery,为什么还要在加载的页面中包含 jQuery?
    • 因为在不加载时很少有功能工作,在重新加载后很少有功能......但这在 bootstrap.js 的 cas 中不是 jquery 的情况
    • 对不起,我不明白。在任何情况下,您都知道无论您通过加载注入到 div 中的任何内容,它都可以使用已包含的页面的所有包含内容,对吗?因此,例如,如果您的页面 A 包含 jQuery.1.10.js,然后在某些时候将页面 B 加载到 div 容器中,则页面 B 的所有代码都可以使用 jQuery.1.10.js
    • 是的,但请查看引导程序案例:我在页面 a 和 b 中都有引导程序,当我在页面 a 中包含页面 b 时,页面 a 中的引导菜单停止工作,但如果我不使用b 中的引导程序并根据您的需要包括引导程序已经存在菜单工作,但页面 b 中的引导程序模式停止工作。如果我在 b modals 中包含引导程序,但菜单停止工作,这就是问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 2016-08-12
    • 2010-12-20
    相关资源
    最近更新 更多