Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.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的ajax会自动运行脚本?_Javascript_Jquery_Ajax_Iframe_Jquery Plugins - Fatal编程技术网

Javascript 为什么jQuery的ajax会自动运行脚本?

Javascript 为什么jQuery的ajax会自动运行脚本?,javascript,jquery,ajax,iframe,jquery-plugins,Javascript,Jquery,Ajax,Iframe,Jquery Plugins,我最近注意到,如果在将jQuery注入内部iframe之后立即调用jQueryAjax,jQuery将丢失其功能,如jQuery…dialog、.Dragable和任何其他插件。如果ajax调用被注释掉,jQuery就可以正常工作。这是一个已知的错误,还是我做错了什么?此文件中可以看到此问题,jQuery位于同一目录中: <html> <head> <link rel="stylesheet" href="http://code.jquery.com/ui/

我最近注意到,如果在将jQuery注入内部iframe之后立即调用jQueryAjax,jQuery将丢失其功能,如jQuery…dialog、.Dragable和任何其他插件。如果ajax调用被注释掉,jQuery就可以正常工作。这是一个已知的错误,还是我做错了什么?此文件中可以看到此问题,jQuery位于同一目录中:

<html>
<head>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
    <script src="jquery.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
</head>
<body>

Try and <button id="btn">load</button>
<iframe width=300 height=300></iframe>

<script>
"use strict";
jQuery('#btn').click(function(){
    var $ = jQuery;
    console.log(typeof jQuery('iframe').dialog);
    var doc = jQuery('iframe')[0].contentDocument;
    function insertscript(src) {
        var newscript = doc.createElement('script');
        newscript.setAttribute('src',src);
        doc.documentElement.appendChild(newscript);
    }
    insertscript('jquery.js');

    //This breaks the jQuery plugins:
    var test = $.get('jquery.js',function(){
        //Now we know jQuery should be in the frame.
    });

    //So does this:
    //jQuery.ajax({url:'http://192.168.1.17/wordpress/wp-includes/js/jquery/jquery.js',cache:true,processData:false});

    console.log(typeof jQuery('iframe').dialog);
    window.setTimeout(function(){
        //jQuery is no longer the original jQuery object. Note the cached reference $().dialog does exist though.
        console.log('after awhile... dialog is ' + typeof jQuery('iframe').dialog);
    },3000)
    //jQuery.ajax({url:jqurl,cache:true,processData:false});
});
</script>
</body></html>
这是问题的一个最小示例,确保iframe已经加载了某个jQuery.js,那么ajax应该在向iframe添加其他内容之前缓存脚本

单击“加载”,过一会儿,控制台日志将在一段时间后显示。。。对话框未定义-仅当使用ajax时


更新:看起来$.get'jquery.js'实际运行脚本。$。get'alert.js'在alert.js具有警报功能时显示警报。对于jQuery,重新定义全局jQuery引用。为什么jQuery的ajax有这种行为?所有ajax实现都会发生这种情况吗?

正如前面有人回答说,他的答案被删除了一样,jQuery ajax会根据您请求的内容类型自动选择要执行的操作。不幸的是,文档不足的特性。加载外部js不仅会在浏览器获取脚本时返回,还会运行脚本

无论何时在以后重新包含jQuery,它都会重写window.jQuery对象,从而删除jQuery.prototype.dialog等

在这种情况下,Firefox.watch函数很有帮助,它可以帮助您了解哪些内容被重新定义。例如,这将为您提供重新定义jQuery的任何内容的堆栈跟踪:

window.watch('jQuery',function() { console.trace() } )

你是说让脚本应用于动态内容吗。。。?或者别的什么?如果你能在JSFIDLE或其他东西中把它转换成一个简化的测试用例,那么测试错误会更容易。另外,为什么要运行对jquery.js的ajax调用?我尝试了JSFIDLE,但它似乎需要相同的域。我是用ajax加载的,所以我知道以后插入插件脚本会起作用,因为我有jQuery。如果你再次加载jQuery…将删除绑定到初始jQuery对象的所有现有插件。覆盖初始jQuery对象。应该从解释情况开始。