我写了个js插件changeCss.js:$(document).ready(function () {
    $("#header").css("color", "red");
    ...
    ...
    ...
});
插件依赖于jquery,有时候,别人使用这个插件,没有引入jquery.js文件,我就想把jquery.js的引入放到changeCss.js里处理。
如果html没有引入jquery.js,那么,changeCss.js就加载jquery.js.怎么实现呢?

解决方案 »

  1.   


    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="gb2312" />
    <title></title>
    <style>
    </style>
    <script>
    window.jQuery ? loadJQ = true : loadJQ =false;
    var myJQ = function(){
    jQuery(function(){
    alert( $('#test').html() );
    })
    };
    if(loadJQ){
    myJQ();
    }else{
    var s = document.createElement('script');
    s.src = 'http://code.jquery.com/jquery-latest.js';
    document.getElementsByTagName('head')[0].appendChild(s);
    if(s.readyState){
    s.onreadystatechange = function(){
    if(s.readyState == 'complete' || s.readyState == 'loaded'){
    s.onreadystatechange = null;
    myJQ();
    }
    }
    }else{
    s.onload = function(){
    myJQ();
    }
    }
    }
    </script>
    </head>
    <body>
    <div id="test">测试</div>
    </body>
    </html>
      

  2.   

    (function () {
    if (typeof ($) == "undefined") {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";
    document.getElementsByTagName('head')[0].appendChild(script);
    }
    })();
      

  3.   

    楼上很给力啊!!!!
    js文件里面不能引入js。
    楼主的王道是把你用的jquary的js包含在你的插件的文件夹里面。用插件的时候就直接调用你的插件文件夹里面的js。