<html>
<head>
<script src="../js/jquery-1.4.2.min.js" type="text/javascript"></script><script>
//function loadJs(){
var script = document.createElement('script');
script.type = 'type/javascript';
script.src = 'test.js';
script.id = 'loadJsID';
document.getElementsByTagName('head')[0].appendChild(script);// jQuery.getScript("test.js",function(){});
//}function clickMe(){
// alert(document.getElementById("loadJsID").textContent);
// alert(document.getElementById("loadJsID").src);
test();
}//window.onload = loadJs;</script></head>
<body>
<h1>javascript and css  load test.</h1>
<input type="button" value="Click Me" onclick="clickMe();">
</body></html>test.js:
function test(){
alert(9999999);
}test();我在网上看了一下,多数也是说是这么加载的,但我在IE8,FF3.6下均没有加载成功。而用
jQuery.getScript("test.js",function(){});则可以加载。
请教失败原因,还有就是jquery加载的原理。还有就是动态加载CSS要怎么办。谢谢。

解决方案 »

  1.   

    可能是浏览器兼容问题,试试IE6看看。jquery在浏览器兼容方法做得很出色。
      

  2.   

    动态加载要注意页面加载延迟,把它追加到body里就好了function loadJs(){
    var script = document.createElement('script');
    script.type = 'type/javascript';
    script.src = 'test.js';
    script.id = 'loadJsID';
    ddocument.body.appendChild(script);}
      

  3.   


    <script>
    //function loadJs(){
    var script = document.createElement('script');
    //script.type = 'type/javascript';
    script.src = 'test.js';
    script.id = 'loadJsID';
    document.getElementsByTagName('head')[0].appendChild(script);// jQuery.getScript("test.js",function(){});
    //}function clickMe(){
    // alert(document.getElementById("loadJsID").textContent);
    // alert(document.getElementById("loadJsID").src);
    test();
    }//window.onload = loadJs;</script>//script.type = 'type/javascript';
    去掉这句。
      

  4.   

    #4楼的太强大了。
    //script.type = 'type/javascript';
    去掉这句。
    确实是可以了。但为什么要除掉这个呢?
    谢谢各位了。
      

  5.   

    script.type = 'text/javascript';
      

  6.   

    script.type = 'type/javascript';
    发现一处明显的错误
    是text/javascript不是type/javascript
      

  7.   

    对的哦。好像是我的type写错了。但我在公司时写对了也没有起作用。谢谢各们。结贴。