Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.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_Javascript_Jquery_Web_Scripting - Fatal编程技术网

在普通javascript中动态加载jquery

在普通javascript中动态加载jquery,javascript,jquery,web,scripting,Javascript,Jquery,Web,Scripting,我使用的页面在dom中没有引用jquery。我需要使用一些jquery功能,因此我认为最好的办法是将jquery注入现有dom。我从和得到了函数的概念……这些解决方案似乎都不适合我。我仍然收到一个错误,无法识别jquery选择器 (function() { function getScript(url, success) { var script = document.createElement('script'); script.src = url;

我使用的页面在dom中没有引用jquery。我需要使用一些jquery功能,因此我认为最好的办法是将jquery注入现有dom。我从和得到了函数的概念……这些解决方案似乎都不适合我。我仍然收到一个错误,无法识别jquery选择器

(function() {
    function getScript(url, success) {
        var script = document.createElement('script');
        script.src = url;
        var head = document.getElementsByTagName('head')[0],
            done = false;
        // Attach handlers for all browsers
        script.onload = script.onreadystatechange = function() {
          if (!done && (!this.readyState
               || this.readyState == 'loaded'
               || this.readyState == 'complete')) {
            done = true;
            success();
            script.onload = script.onreadystatechange = null;
            head.removeChild(script);
          }
        };
        head.appendChild(script);
    }
    getScript('https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js',function() {
        // Yay jQuery is ready \o/
    });                         


    cards = $('div:first');
    $('body').empty().append(cards);

    // Delete first and second child divs

    first = $('div:first div:first');
    $('div:first div:first').css('position', '').css('left', '').css('z-index', '').css('height', '250px').remove();

    second = $('div:first div:first');
    $('div:first div:first').css('position', 'relative').css('left', '').css('z-index', '').remove();

    $('body').empty().append(first).append(second);
    })();

您应该将自己的代码移动到回调中,它会显示//Yay。。。。只有这样,代码才会在jQuery加载后运行

getScript('https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js', function() {
    // Yay jQuery is ready \o/

    $('body').empty().append($('div:first'));
    // Delete first and second child divs
    for (var i = 0; i < 2; i++) {
        $('div:first div:first').remove();
    }
});                         
getScript('https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js,函数(){
//是的jQuery已准备就绪\o/
$('body').empty().append($('div:first'));
//删除第一个子div和第二个子div
对于(变量i=0;i<2;i++){
$('div:first div:first')。删除();
}
});                         

您应该将自己的代码移动到回调中,它会显示
//Yay…
。只有这样,代码才会在jQuery加载后运行;l那太尴尬了……请随意回答,我会接受脚本元素上没有“onreadystatechange”这样的属性,我认为这是误导性的信息,只要“onload”就足够了。在这里,Chris G的回答是可以接受的。这是一个同样的打劫