3

我尝试了很多方法,但无法弄清楚如何做到这一点。

我在许多 html 文件的标题中加载了许多外部脚本。这是一个例子:

<script type="text/javascript"  src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({  tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>

<script type="text/javascript" language="Javascript" 
        src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js">
</script>

<script type="text/javascript" src="http://www.wolfram.com/cdf-player/plugin/v2.1/cdfplugin.js"></script>

避免在每个 HTML 文件中一次又一次地复制和粘贴这些内容的最佳方法是什么?

我无法将所有这些放在一个 common.js 文件中并包含它,因为它们已经包含<script>在其中。即我不能这样做

<script src="common.js"></script>

知道怎么做吗?可能是jQuery?(我对此一无所知)。可能这有关系吗?Jquery load() 一个包含 JavaScript 的 html 文件

谢谢

4

1 回答 1

2

对于要加载的每个源,您可以在 common.js 中多次使用以下命令。只是不断改变node.src价值。

    var node = document.createElement('script');
    node.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js";
    document.body.appendChild(node);

您可以选择在bodyiedocument.body.appendChild(node);headiedocument.head.appendChild(node);的 html中添加脚本的位置

于 2013-01-20T12:47:35.667 回答