Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 让jQuery在外部js文件中工作时出现问题_Javascript_Jquery_External - Fatal编程技术网

Javascript 让jQuery在外部js文件中工作时出现问题

Javascript 让jQuery在外部js文件中工作时出现问题,javascript,jquery,external,Javascript,Jquery,External,我刚刚开始使用jQuery,并且很幸运地使它能够在实际的aspx和html文件中工作,但现在我正在尝试在外部js文件中工作 在头部的html文件中,我有: <!--Declare jQuery files--> <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.js"></script> <script type="text/ja

我刚刚开始使用jQuery,并且很幸运地使它能够在实际的aspx和html文件中工作,但现在我正在尝试在外部js文件中工作

在头部的html文件中,我有:

 <!--Declare jQuery files-->
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1-vsdoc.js"></script>

<!--Declare external java files-->
<script type="text/javascript" src="javascript/SiteFunction.js"></script>

所以,我完全不知所措

首先,您应该只包含jQuery一次。你链接到的那些文件都有相同的JS代码(除了一个是缩小的,另一个有附加注释)

这就是您所需要的:

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.min.js"></script>
如果
newPage
只是一个ID,那么:

$('<p>Test</p>').appendTo(newPage);
$('Test

)。附录(newPage);
应该是这样的:

$('<p>Test</p>').appendTo('#' + newPage);
$('Test

)。附录('#'+newPage);
首先,您应该只包含一次jQuery。你链接到的那些文件都有相同的JS代码(除了一个是缩小的,另一个有附加注释)

这就是您所需要的:

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.min.js"></script>
如果
newPage
只是一个ID,那么:

$('<p>Test</p>').appendTo(newPage);
$('Test

)。附录(newPage);
应该是这样的:

$('<p>Test</p>').appendTo('#' + newPage);
$('Test

)。附录('#'+newPage);
链接外部javascript/jquery文件时,不必声明type=”“


也可以将它们加载到所有正文内容的底部,以获得更好的加载时间。

链接外部javascript/jquery文件时,不必声明type=”“


也让他们在所有正文内容的底部加载,以获得更好的加载时间。

我使用ASP.NET和嵌套母版页,这对我来说是一个真正的问题。我在拿文件,准备的东西一直丢了。我想我需要一个更好的方法,我想到的是创建一个数组来保存我想要调用的所有函数,并在一个文档中。准备好了,循环该数组,按照添加的顺序调用每个函数。如果由于不同的原因使用了多个包含在不同位置的外部文件,这将允许您轻松地执行此操作:

<!-- your external javascript file -->
<script type="text/javascript">

    // create array to hold a list functions to run once
    var oneTimeFunctions = new Array;


    // create variable to store first function
    var test1 = function() { $('<p>Test</p>').append("#" + newPage); };

    // add first function to the end of the array
    oneTimeFunctions[oneTimeFunctions.length] = test1;


    // create variable to store second function
    var test2 = function() {

        alert(newPage);
        $('<p>Test</p>').appendTo(newPage);

        $(newPage).animate({ left: '0px' }, 2000, function() {
            // Animation complete.
            alert("animated newPage");
        });

        $(currentPage).animate({ right: '0px' }, 2000, function() {
            // Animation complete.
        });

    };

    // add second function to the end of the array
    oneTimeFunctions[oneTimeFunctions.length] = test2;


    // call document.ready only once
    $(function(){

        // call each function that was added to the oneTimeFunctions array
        $.each(oneTimeFunctions, function(index, func) { func(); });

    });

</script>

//创建数组以保存要运行一次的列表函数
var oneTimeFunctions=新数组;
//创建变量以存储第一个函数
var test1=function(){$('Test

).append(“#”+newPage);}; //将第一个函数添加到数组末尾 oneTimeFunctions[oneTimeFunctions.length]=test1; //创建变量以存储第二个函数 var test2=函数(){ 警报(新页面); $(“Test

”)。附录(新页); $(newPage).animate({left:'0px'},2000,function(){ //动画完成。 警报(“动画新页面”); }); $(currentPage).animate({right:'0px'},2000,function(){ //动画完成。 }); }; //将第二个函数添加到数组的末尾 oneTimeFunctions[oneTimeFunctions.length]=test2; //只调用document.ready一次 $(函数(){ //调用添加到oneTimeFunctions数组中的每个函数 $.each(oneTimeFunctions,function(index,func){func();}); });

现在,如我所说,您可以将其拆分为多个外部文件。这只是显示了如何按顺序处理它。您只需首先创建数组,然后创建函数并将它们添加到各种外部文件中的数组中,最后运行document.ready部分,该部分循环调用每个函数。

我使用ASP.NET和嵌套母版页,这对我来说是一个真正的问题。我在拿文件,准备的东西一直丢了。我想我需要一个更好的方法,我想到的是创建一个数组来保存我想要调用的所有函数,并在一个文档中。准备好了,循环该数组,按照添加的顺序调用每个函数。如果由于不同的原因使用了多个包含在不同位置的外部文件,这将允许您轻松地执行此操作:

<!-- your external javascript file -->
<script type="text/javascript">

    // create array to hold a list functions to run once
    var oneTimeFunctions = new Array;


    // create variable to store first function
    var test1 = function() { $('<p>Test</p>').append("#" + newPage); };

    // add first function to the end of the array
    oneTimeFunctions[oneTimeFunctions.length] = test1;


    // create variable to store second function
    var test2 = function() {

        alert(newPage);
        $('<p>Test</p>').appendTo(newPage);

        $(newPage).animate({ left: '0px' }, 2000, function() {
            // Animation complete.
            alert("animated newPage");
        });

        $(currentPage).animate({ right: '0px' }, 2000, function() {
            // Animation complete.
        });

    };

    // add second function to the end of the array
    oneTimeFunctions[oneTimeFunctions.length] = test2;


    // call document.ready only once
    $(function(){

        // call each function that was added to the oneTimeFunctions array
        $.each(oneTimeFunctions, function(index, func) { func(); });

    });

</script>

//创建数组以保存要运行一次的列表函数
var oneTimeFunctions=新数组;
//创建变量以存储第一个函数
var test1=function(){$('Test

).append(“#”+newPage);}; //将第一个函数添加到数组末尾 oneTimeFunctions[oneTimeFunctions.length]=test1; //创建变量以存储第二个函数 var test2=函数(){ 警报(新页面); $(“Test

”)。附录(新页); $(newPage).animate({left:'0px'},2000,function(){ //动画完成。 警报(“动画新页面”); }); $(currentPage).animate({right:'0px'},2000,function(){ //动画完成。 }); }; //将第二个函数添加到数组的末尾 oneTimeFunctions[oneTimeFunctions.length]=test2; //只调用document.ready一次 $(函数(){ //调用添加到oneTimeFunctions数组中的每个函数 $.each(oneTimeFunctions,function(index,func){func();}); });

现在,如我所说,您可以将其拆分为多个外部文件。这只是显示了如何按顺序处理它。您只需首先创建数组,然后创建函数并将它们添加到各种外部文件中的数组中,最后运行document.ready部分,该部分循环调用每个函数。

您在Javascript调试器中看到任何错误吗?如果是,那又怎样?您不需要同时包含ajax/jQuery/jQuery-1.5.1.min.js和ajax/jQuery/jQuery-1.5.1.js,它们是一个整体。添加两者会占用加载时间。您在Javascript调试器中看到任何错误吗?如果是,那又怎样?您不需要同时包含ajax/jQuery/jQuery-1.5.1.min.js和ajax/jQuery/jQuery-1.5.1.js,它们是一个整体。两者相加将占用加载时间。。。。为了澄清,我没有检查jQuery以确保它在语法上是正确的。我将您的代码复制/粘贴到我的方法示例中,以使其更适用于您。。。。我只是想澄清一下,我没有检查您的jQuery以确保它是syntac