删除< link>元素与jQuery? [英] Removing <link> element with jQuery?

查看:150
本文介绍了删除< link>元素与jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想使用style.css中的样式,所以我决定从DOM中删除style.css。这个工作在Firefox和IE8中很好,但在IE6中不行:

I don't want to use styles from style.css, so I decided to remove style.css from DOM. This work just fine in Firefox and IE8, but not in IE6:

$("LINK[href='http://www.example.com/style.css']").remove();

使用jQuery的任何其他解决方案?

Any other solution, with jQuery?

以下是示例:

HTML:

Here is example:
HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Testing</title>
<script type="text/javascript" src="path/to/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $("link[href*='style.css']").remove();         
});
</script>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="content">...</div>
</body>
</html>

这里是CSS(style.css):

And here is CSS (style.css):

#content {
    background-color:#333;
}

只有在IE #content仍然是黑暗的。 (

也许是jQuery错误?

Only in IE #content is still dark. :(
Maybe is jQuery bug?

推荐答案

它是IE渲染引擎的一个错误(或可能是一个功能)。

似乎这个问题是由Internet Explorer在从DOM中移除LINK元素后,无法正确地重新呈现网页。

It seems this problem is being caused by the fact that Internet Explorer does not correctly re-render the page after removing the LINK element from the DOM.

在这种特殊情况下,LINK标记不再出现在DOM,显示已加载到内存中的CSS

In this particular case, the LINK tag is no longer present at the DOM, but IE still displays the CSS that has been loaded into memory.

解决方法是使用 .disabled / code>属性如下:

// following code will disable the first stylesheet
// the actual DOM-reference to the element will not be removed; 
// this is particularly useful since this allows you to enable it
// again at a later stage if you'd want to.
document.styleSheets[0].disabled = true;

回复您的评论:

或者,如果您要通过href删除它,请使用以下代码:

Or, if you want to remove it by the href use the following code:

var styleSheets = document.styleSheets;
var href = 'http://yoursite.com/foo/bar/baz.css';
for (var i = 0; i < styleSheets.length; i++) {
    if (styleSheets[i].href == href) {
        styleSheets[i].disabled = true;
        break;
    }
}

这篇关于删除&lt; link&gt;元素与jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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