首页 > 使用jquery插入<script>元素不加载src对应的资源文件是什么原因?

使用jquery插入<script>元素不加载src对应的资源文件是什么原因?

window.onload=function()
{
    使用jquery方法创建的script元素不会下载/js/editor/editor.js文件
    $('body')[0].insertBefore($('<script type="text/javascript" src="/js/editor/editor.js"><\/script>')[0],$('#jquery').next('script')[0]);

    下面的做法可以下载文件/js/editor/editor.js文件
    var body=$('body')[0];
    var script=document.createElement('script');
    script.src="/js/editor/editor.js";
    body.insertBefore(script,$('#jquery').next('script')[0]);
};

请问这是什么原因?


你的第一种做法, 并不是使用 jQuery 提供的方法添加的, 而只是通过 jQuery 对象对应的原生的DOM对象及相应的方法完成的.

何必写那么麻烦呢, 直接用下面这样的, 简单明了.

$('body').append('<script src="/js/editor/editor.js"></sc'+'ript>');

你创建dom为什么要加[0]

【热门文章】
【热门文章】