[英]How to change CSS file or hide <link> tag
I want to change the design of my site by changing the CSS file attached. 我想通过更改附加的CSS文件来更改网站的设计。 I have tried with script when the link is with id "link" 当链接的ID为“ link”时,我尝试使用脚本
var x = document.getElementByID ("link")
X.href = style2
It didn't work. 没用
The other thing I tried was to hide the <link>
tag which had class "linkclass" 我尝试的另一件事是隐藏具有类“ linkclass”的<link>
标记
<style>
link.linkclass {
visibility:hidden;
}
</style>
But it didn't work either. 但这也不起作用。
Can someone help. 有人可以帮忙吗?
Sorry if the code is bad formatted but I can't get how to format code in stack overflow 抱歉,如果代码格式错误,但是我无法在堆栈溢出中格式化代码
Three things wrong with this: 这有三点错误:
X
is a different variable than x
这意味着X
是与x
不同的变量 style2
is not a valid URL. style2
不是有效的网址。 You have to use an URL to an existing .css file 您必须使用指向现有.css文件的URL <link>
is not a visible element. <link>
不是可见的元素。 Hiding an element that isn't visible in the first place accomplishes nothing. 隐藏最初不可见的元素不会产生任何效果。 This works: 这有效:
var x = document.getElementByID("link");
x.href = "http://url/to/your/style2.css";
// ^ notice the lowercase x
You could do 你可以做
$("#link").disabled = true;
This may also work. 这也可能起作用。
document.getElementByID("link").disabled = true;
There is also another Stack question that addresses this here. 这里还有另一个堆栈问题可以解决。 Removing or Replacing a Stykesheet 卸下或更换Stykesheet
update 更新
You say you are trying to change the stylesheet. 您说您正在尝试更改样式表。 You could create a function to do it like this. 您可以创建一个函数来做到这一点。
function styleSheetSwitcher( newFile ){
$("#link").prop("href", newFile);
}
styleSheetSwitcher("myNewCss.css");
If you wanna hide element (I got that impression from your examples), your javascript code should look like this: 如果您想隐藏元素(我从您的示例中得到了印象),您的javascript代码应如下所示:
var x = document.getElementById("link");
x.style.display = 'none';
Also take care with following: 还要注意以下几点:
-uppercase letters getElementbyId -大写字母getElementbyId
-you're missing semicolon (;) after first expression -您在第一个表达式后缺少分号(;)
-your variable "x" is uppercase in second row("X"). -您的变量“ x”在第二行(“ X”)中为大写。
In most cases this should be enough to disable element with CSS, just add this class (linkclass) to element which you want to hide: 在大多数情况下,这足以禁用CSS元素,只需将此类(linkclass)添加到要隐藏的元素中:
<style>
.linkclass {
display: none;
}
</style>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.