当脚本src添加到head时,不会调用jQuery函数


jQuery function is not called when script src is added to head

我对源代码等有点新手,我理所当然地认为我可以简单地将脚本src添加到头部,然后添加我想要的任意多的脚本或函数。在大多数情况下,这都很好,但现在我有一个问题无法解决。

我可以运行一个脚本,也可以运行另一个,无论我尝试了多少种不同的排列,两者都无法协调工作。我相信有一个简单的解决办法,或者我完全错过了什么。我在网上找了一本白痴指南,但没有找到任何有用的东西。

这两个函数脚本位于不同的位置。一个在头部,从HTML页面的主体调用,另一个在include中,该include被添加到每个页面以进行动态链接。

这加载了除体内以外的所有内容

<!--Creation and hiding of div to allow images load-->
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="../js/helperFunctions.js"></script>
<!-- jQuery (required) -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="../js/jquery.min.js"><'/script>')</script>
<!-- Syntax highlighting -->
<link rel="stylesheet" href="../mainCSS/prettify.css" media="screen">
<script src="../js/prettify.js"></script>
<!-- AnythingSlider -->
<link rel="stylesheet" href="../mainCSS/anythingslider.css">
<script src="../js/jquery.anythingslider.js"></script>
<script>
    $(function(){
            prettyPrint script here;
</script>
<script>
    $(function(){
            slider script here
</script>

虽然这加载了正文脚本,但没有加载其他脚本。:

<!-- jQuery (required) -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="../js/jquery.min.js"><'/script>')</script>
<!-- Syntax highlighting -->
<link rel="stylesheet" href="../mainCSS/prettify.css" media="screen">
<script src="../js/prettify.js"></script>
<!-- AnythingSlider -->
<link rel="stylesheet" href="../mainCSS/anythingslider.css">
<script src="../js/jquery.anythingslider.js"></script>
<script>
    $(function(){
            prettyPrint script here;
</script>
<script>
    $(function(){
            slider script here
</script>
   <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
   <script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
   <script src="../js/helperFunctions.js"></script>

然后我试着把脚本src放在include中,包括两次src和许多其他愚蠢愚蠢的想法。

如果有人能帮忙,那就太好了!非常感谢

您在这段代码中像三次包含jQuery。我不确定这是不是问题所在,但这不是的解决方案

// jquery from jquery.com
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<!-- jQuery (required) --> // jQuery from google
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="../js/jquery.min.js"><'/script>')</script>
// both of these are expendable

这可能会导致冲突,因为最后包含的是库的旧版本。你只需要包容。我假设您使用migrate是因为您的脚本中运行了较旧的jQuery代码。

代码错误

<script>
$(function(){ // syntax error
        prettyPrint script here;
</script>
<script>
$(function(){ // same error
        slider script here
</script>

这里有语法错误,在这段代码之后的任何脚本都将无法运行或工作。

我在其中一个脚本中有一个函数返回错误。这似乎停止了代码的est

现在头上只有一个jquery实例。