Jquery plugins 是否将jQuery插件包含在.getScript()中?

Jquery plugins 是否将jQuery插件包含在.getScript()中?,jquery-plugins,jquery,Jquery Plugins,Jquery,我正在尝试使用jQuery.getScript加载jQuery模板插件,但即使它执行了请求并且文件正确输入(将url替换为测试JS并且javascript的计算结果正确),jQuery.tmpl仍然没有定义,但是如果我在Chrome的控制台中运行jQuery.tmpl,它仍然是一个函数 示例代码: <script type="text/javascript"> jQuery.getScript('http://ajax.microsoft.com/ajax/jquery.te

我正在尝试使用jQuery.getScript加载jQuery模板插件,但即使它执行了请求并且文件正确输入(将url替换为测试JS并且javascript的计算结果正确),jQuery.tmpl仍然没有定义,但是如果我在Chrome的控制台中运行jQuery.tmpl,它仍然是一个函数

示例代码:

<script type="text/javascript">    
jQuery.getScript('http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js');
console.log(jQuery.tmpl); // returns undefined.
</script>

getScript('http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js');
console.log(jQuery.tmpl);//返回未定义的。

知道问题是什么吗?

您应该在
getScript

jQuery.getScript('http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js',
function(){
   console.log(jQuery.tmpl);
});

您应该在
getScript

jQuery.getScript('http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js',
function(){
   console.log(jQuery.tmpl);
});

您的问题是脚本正在异步加载,这意味着您需要等待脚本完成后才能使用它

解决方案如下:

$.getScript('.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js', function (data, textStatus) {
    //Use the script now
});

如果您需要立即使用脚本,我建议您只需在页面上添加对脚本的引用。

您的问题是脚本正在异步加载,这意味着您需要等待脚本完成后才能使用它

解决方案如下:

$.getScript('.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js', function (data, textStatus) {
    //Use the script now
});

如果您需要立即在页面上添加对脚本的引用,我建议您只需在页面上添加对脚本的引用。

在下一条语句立即执行时,您需要将其包装在回调中,而脚本加载可能需要一些时间:

jQuery.getScript('http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js', function() {
   console.log(jQuery.tmpl);
});

当下一条语句立即执行时,您需要将其包装在回调中,而脚本加载可能需要一些时间:

jQuery.getScript('http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js', function() {
   console.log(jQuery.tmpl);
});

您应该等到脚本加载完毕

jQuery.getScript('http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js',
    function(){
        console.log(jQuery.tmpl);
    }
);

您应该等到脚本加载完毕

jQuery.getScript('http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js',
    function(){
        console.log(jQuery.tmpl);
    }
);

等待它加载。尝试使用:

jQuery.getScript('http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js', function() {
    console.log(jQuery.tmpl);
});
或者,为了使其更具可读性,请尝试使用:

$.ajax({
    url: "http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js",
    dataType: "script",
    success: function() {
        console.log(jQuery.tmpl);   
    }
});

等待它加载。尝试使用:

jQuery.getScript('http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js', function() {
    console.log(jQuery.tmpl);
});
或者,为了使其更具可读性,请尝试使用:

$.ajax({
    url: "http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js",
    dataType: "script",
    success: function() {
        console.log(jQuery.tmpl);   
    }
});

我最终只是同步了.ajax调用,以便将其作为逻辑流的一部分:

jQuery.ajax({
    url: script,
    async: false,
    dataType: "script",
    success: function() {
        console.log("Loaded "+script);   
    },
    complete: function() {
        console.log("Failed to load "+script);   
    }
});

我最终只是同步了.ajax调用,以便将其作为逻辑流的一部分:

jQuery.ajax({
    url: script,
    async: false,
    dataType: "script",
    success: function() {
        console.log("Loaded "+script);   
    },
    complete: function() {
        console.log("Failed to load "+script);   
    }
});

同步或异步扩展方法到$。getScript:

同步或异步扩展方法到$。getScript:

起初非常惊讶:)起初非常惊讶:)