如何在IE使用jQuery附加样式表? [英] How to append Style Sheets in IE using jQuery?

查看:101
本文介绍了如何在IE使用jQuery附加样式表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我只是想创建一个插件,我需要它是用户友好的,所以我想附加头上的< link /> >部分的用户页面作为我的插件加载。它适用于所有其他浏览器(不确定IE9,IE7和IE6),但不工作的IE8!
我不知道我的插件是什么错误,所以我只是创建了一个示例页面,面对类似的问题!这里是我的测试页HTML + jQuery代码------

I was just trying to create a plugin, and I needed it to be user-friendly so I want to append the <link/> tag on the head portion of the users page as my plugin is loaded. It works fine with all the other browsers (not sure of IE9, IE7 & IE6) but doesn't work for IE8! I don't what is getting wrong with my plugin, so I just created a sample page and faced the similar problem! Here is my test page HTML+jQuery Code ------

<html>
       <head>
              <script src="../jquery-1.6.min.js"></script>
              <script>
                       $(document).ready(function () {
                          $(document.head).append('<link rel="stylesheet" type="text/css" href="style.css" />');
                       });
              </script>
       </head>
       <body>
              <h1>Text!</h1>
       </body>
</html>

这里是我的CSS代码-----

And here is my CSS code -----

body {
 background:#ddd; 
}
h1 {
 color: #789; 
}

因此任何人都可以告诉我我在哪里出错或是这个问题的生命周期?

So can anyone tell me where I'm going wrong or is this the problem of lifetime?

预先感谢

在看到第一条评论并提供链接后,我刚刚创建了这个示例代码,发现了一些惊人的!看到这个~~~

After seeing the first comment and going to link supplied, I just created this sample code and found something amazing! See this ~~~

<html>
       <head>
              <script src="../jquery-1.6.min.js"></script>
              <script>
                       $(document).ready(function () {
                          if (document.getElementsByTagName('head')[0] === document.head) {
                           $("head").append('<link rel="stylesheet" type="text/css" href="style.css" />');
                          }else {
                           alert('This doesn\'t supports head appending!');
                          }
                       });
              </script>
       </head>
       <body>
              <h1>Text!</h1>
       </body>
</html>

在使用IE8浏览器执行此页面时,我收到

On executing this page with my IE8 browser I get the message that

This doesn't supports head appending!

好,我的浏览器怎么回事,还是IE8的错?

Well I don't what is wrong with my browser or is this the fault of IE8?

推荐答案

if (document.createStyleSheet)
{
    document.createStyleSheet("style.css");
}
else
{
    $("head")
       .append('<link rel="stylesheet" type="text/css" href="style.css" />'); 
}

这篇关于如何在IE使用jQuery附加样式表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆