2012-04-15 43 views
13

Tôi có một tập lệnh chạy trong phần đầu của tài liệu html và nó hoạt động trong mọi trình duyệt ngoại trừ trình khám phá internet. Thử nghiệm trong Opera, Safari, Chrome, Firefox, Internet Explorer.document.head.appendChild hoặc document.createElement không hoạt động trong IE

Mã của tôi là như sau:

<html> 
    <head> 
    <script type = "text/javascript"> 
     var date = new Date(); 
     var month = date.getMonth() + 1; 
     if (month >= 3 && month <= 5) 
     { 
     var NewScript = document.createElement("script"); 
     NewScript.type = "text/javascript"; 
     NewScript.src = "source1.js"; 
     var NewStyles = document.createElement("link"); 
     NewStyles.rel = "stylesheet"; 
     NewStyles.type = "text/css"; 
     NewStyles.href = "css1.css"; 
     document.head.appendChild(NewScript); 
     document.head.appendChild(NewStyles); 
     } 
     else 
     { 
     var NewScript = document.createElement("script"); 
     NewScript.type = "text/javascript"; 
     NewScript.src = "source2.js"; 
     var NewStyles = document.createElement("link"); 
     NewStyles.rel = "stylesheet"; 
     NewStyles.type = "text/css"; 
     NewStyles.href = "css2.css"; 
     document.head.appendChild(NewScript); 
     document.head.appendChild(NewStyles); 
     } 
    </script> 
    </head> 
    <body> 
    <!-- MY CONTENT GOES HERE --> 
    </body> 
</html> 

Tôi không chắc chắn nếu đó là document.createElement hoặc document.head.appendChild không hoạt động trong IE. Như đã nêu trước đây, nó hoạt động trong tất cả các trình duyệt khác mà tôi đã thử nghiệm. Trợ giúp với điều này sẽ được đánh giá rất cao vì tôi sẽ tiếp tục tự tìm ra vấn đề/giải pháp. Cảm ơn!

Trả lời

34

Hãy thử document.getElementsByTagName('head')[0] thay vì document.head

+0

Oh my gosh! Bạn là một người tiết kiệm cuộc sống! Tôi đã thử gần như mọi thứ nhưng thế. Tôi đang tạo ra các chủ đề khác nhau dựa trên các mùa trong năm, sau đó dự án tiếp theo của tôi là chủ đề kỳ nghỉ. Cảm ơn một lần nữa! –

Các vấn đề liên quan