【问题标题】:How to include a js file for a different version of jquery (one that supports IE8)?如何为不同版本的 jquery(支持 IE8 的版本)包含 js 文件?
【发布时间】:2019-04-30 15:53:03
【问题描述】:

我有一个简单的网页,它只是一些 css 和 html。我需要使用 jquery 包含移动导航,它还需要与 IE8 兼容。

我发现 IE8 支持 1.1.9 版的 jquery,我还发现了如何使用这些不同的版本,具体取决于加载我的网站的浏览器。

我的问题是,如果我使用 IE8 的 jquery 版本,我是否还需要在我的 html 脚本标记中包含不同的 js 文件?谢谢大家的回答

    //this is what i have - before </body>

    <!--[if lt IE 9]>
      <script  src="https://code.jquery.com/jquery-1.9.0.min.js"</script>
    <![endif]-->
    <!--[if gte IE 9]><!-->
      <script src="https://code.jquery.com/jquery-3.4.0.min.js"></script>
    <!--<![endif]--> 

    //and how do I render my js file with my code? is it like this?

    <!--[if lt IE 9]>
      <script  src="https://code.jquery.com/jquery-1.9.0.min.js"</script>
      <script  src="app1.js"</script>

    <![endif]-->
    <!--[if gte IE 9]><!-->
     <script src="app2.js"></script>
    <!--<![endif]-->

【问题讨论】:

  • 你真的死心塌地支持不安全的浏览器吗? IE microsoft.com/en-us/windowsforbusiness/end-of-ie-support 这是一个不安全的浏览器,用户应该升级到更安全的浏览器。

标签: javascript jquery html


【解决方案1】:

我将尝试在此答案中澄清您的问题。

您可以有条件地包含基于 IE 的 HTML 代码(如您所示)。条件不提供范围,因此您无需重新指定已包含的标签。但是,对于 Javascript,您需要在引用它们之前包含依赖文件。因此,如果您在结束 &lt;/body&gt; 之前包含 jquery,那么您应该在其后包含您的代码。

    <!--[if lt IE 9]>
      <script  src="https://code.jquery.com/jquery-1.9.0.min.js"</script>
      <script  src="app1.js"</script>
    <![endif]-->
    <!--[if gte IE 9]>
      <script src="https://code.jquery.com/jquery-3.4.0.min.js"></script>
      <script src="app2.js"></script>
    <![endif]--> 
    </body>

如果您需要将jquery的包含与我们自己的代码的包含分开,则无需重新指定jquery包含:

    <!--[if lt IE 9]>
      <script  src="https://code.jquery.com/jquery-1.9.0.min.js"</script>
    <![endif]-->
    <!--[if gte IE 9]>
      <script src="https://code.jquery.com/jquery-3.4.0.min.js"></script>
    <![endif]--> 

... other stuff here ...

    <!--[if lt IE 9]>
      <script  src="app1.js"</script>
    <![endif]-->
    <!--[if gte IE 9]>
      <script src="app2.js"></script>
    <![endif]--> 
    </body>

请注意,在这些示例中,您不包括用于非 IE 浏览器的 jquery。如果对 IE > 9 的支持与其他浏览器相同,那么您也可以为非 IE 浏览器包含标签。下面,我在顶部使用合并示例:

    <!--[if lt IE 9]>
      <script  src="https://code.jquery.com/jquery-1.9.0.min.js"</script>
      <script  src="app1.js"</script>
    <![endif]-->
    <!--[if gte IE 9]><!-->
      <script src="https://code.jquery.com/jquery-3.4.0.min.js"></script>
      <script src="app2.js"></script>
    <!--<![endif]--> 
    </body>

请注意,从非 IE 浏览器的角度来看,条件标记实际上已被注释掉。

【讨论】:

  • 感谢您的回答!如果我想为非 IE 浏览器包含版本 3.4.0 和 app2.js,我不能将它放在 之后但
猜你喜欢
  • 1970-01-01
  • 2023-01-20
  • 2015-04-19
  • 1970-01-01
  • 1970-01-01
  • 2016-03-03
  • 1970-01-01
  • 2021-10-18
  • 1970-01-01
相关资源
最近更新 更多