jQuery 删除 CSS 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12857734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing the CSS file
提问by ASUR
I am using spring MVC with jsp page for presentation, i have three tab suppose A,Band Cin one jsp page. While clicking on A tab the css file such as aa.css have being loading in head tag with respective div is shown and the same way on click on B and C. The main problem is once the Three.CSSfile is being loaded it overwrite each other. Also i want to remove cssfile from headwhich has being loaded on click of any of above tab using jqueryas shown below.
我正在使用带有 jsp 页面的 spring MVC 进行演示,我在一个 jsp 页面中有三个选项卡,假设A、B和C。单击 A 选项卡时,诸如 aa.css 之类的 css 文件已加载到带有相应 div 的 head 标记中,并且单击 B 和 C 的方式相同。主要问题是一旦三个. CSS文件正在加载它会相互覆盖。此外,我想从头中删除css文件,该文件已在使用jquery单击上述任何选项卡时加载,如下所示。
$("#A").click(function(){
alert("Remove bb and cc.css file form head tag");
});
any idea will help me lot.
任何想法都会对我有很大帮助。
Thanks.
谢谢。
回答by Praveen Kumar Purushothaman
Give an id
to the <link>
tag.
举一个id
到<link>
标签。
<link rel="stylesheet" href="style1.css" id="style1" />
<link rel="stylesheet" href="style2.css" id="style2" />
And use this code:
并使用此代码:
$("#A").click(function(){
$("#style1").attr("disabled", "disabled");
});
Note:While there is no disabled attribute in the HTML standard, there is a disabled attribute on the HTMLLinkElement DOM object.
注意:虽然 HTML 标准中没有 disabled 属性,但 HTMLLinkElement DOM 对象上有一个 disabled 属性。
The use of disabled as an HTML attribute is non-standard and only used by some Microsoft browsers. Do not use it. To achieve a similar effect, use one of the following techniques:
使用 disabled 作为 HTML 属性是非标准的,仅由某些 Microsoft 浏览器使用。不要使用它。要获得类似的效果,请使用以下技术之一:
- If the
disabled
attribute has been added directly to the element on the page, do not include the<link>
element instead; - Set the
disabled
property of the DOM object via scripting.
- 如果该
disabled
属性已直接添加到页面上的元素中,则不要包含该<link>
元素; disabled
通过脚本设置DOM 对象的属性。
回答by Md Toufiqul Islam
You can unload a css by disabling it as follows:
您可以通过禁用 CSS 来卸载它,如下所示:
$("#A").click(function(){
$("link[href*=bb.css]").attr("disabled", "disabled");
$("link[href*=cc.css]").attr("disabled", "disabled");
$("link[href*=aa.css]").removeAttr("disabled");
});
回答by kuhr
I had to disable css files without being able to specify an id, so to remove the following css file:
我不得不在无法指定 id 的情况下禁用 css 文件,因此要删除以下 css 文件:
<link rel="stylesheet" type="text/css" href="http://localhost:8092/bootstrap/css/bootstrap.min.css" disabled="disabled">
I added this script
我添加了这个脚本
<script type="text/javascript">
$(document).ready(function() {
$('link[href*="bootstrap.min.css"]').attr("disabled", "true");
}
</script>
回答by Wahid Masud
You just give that link tag an id or a class (say id="deleteMe") then remove it like below:
您只需为该链接标签指定一个 id 或一个类(比如 id="deleteMe"),然后将其删除,如下所示:
$('head').find('link#deleteMe').remove();
So in your case add id to each file when you link them like this:
因此,在您的情况下,当您像这样链接它们时,将 id 添加到每个文件:
<link id="aa" rel="stylesheet" href="First.css" type="text/css" />
<link id="bb" rel="stylesheet" href="Second.css" type="text/css" />
<link id="bb" rel="stylesheet" href="Third.css" type="text/css" />
Now to remove only Second.css, Third.css you have to write your jQuery like this:
现在只删除 Second.css, Third.css 你必须像这样编写你的 jQuery:
(function ($) {
$('head').find('link#aa').remove();
$('head').find('link#bb').remove();
})(jQuery);