网页上引入jQuery

来源:互联网 发布:经纬度坐标定位软件 编辑:程序博客网 时间:2024/04/29 21:12

需要注意以下一点:

引入的jQuery最好放在所有js最前面。

如图,想要在head中将jQuery引入并放在第一个。

    'use strict';
    var reforeNode = document.getElementsByTagName('script')[0];  
    var script = document.createElement('script');
    script.type="text/javascript";
    script.src = 'http://libs.baidu.com/jquery/2.0.0/jquery.js';
    document.head.insertBefore(script,reforeNode);

这段代码需要注意几点:

(1)引入的jQuery还可以用官方的 http://code.jquery.com/jquery-1.9.1.js,但是我引入的几次,经常在第一次时出现 $ is not defined 的报错,刷新后不再,可能是官网链接的问题,用http://libs.baidu.com/jquery/2.0.0/jquery.js 未见此问题。

(2)document.head.appendChild(script); 之前将jQuery追加到head最后

     parentNode.insertBefore(newNode,oldNode);

    想把新的节点插入到一个已知节点oldNode之前,就要先找到他们的父节点,这样才能把节点放到正确位置。

(3)获取节点方法 详https://www.zhihu.com/question/41328042




0 0
原创粉丝点击