javascript HTML“链接”(样式表)禁用属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10564806/
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
HTML "link" (stylesheet) disabled attribute
提问by VettelS
I'm using JavaScript to enable/disable stylesheets using the following:
我正在使用 JavaScript 使用以下方法启用/禁用样式表:
document.styleSheets[0].disabled = true|false;
This JS works fine, however I would like the stylesheet to be DISabled by default. Whilst I could use the JS above to immediately disable the stylesheet when the page loads, this obviously won't work for users who have JavaScript turned off.
这个 JS 工作正常,但是我希望默认情况下禁用样式表。虽然我可以使用上面的 JS 在页面加载时立即禁用样式表,但这显然不适用于关闭 JavaScript 的用户。
I've tried doing this:
我试过这样做:
<link rel="stylesheet" href="style.css" disabled />
Whilst this disables the stylesheet, JavaScript (or at least the method I'm using) can't ENable it again. I've also tried all of these variations on the "disabled" attribute: disabled="disabled"
, disabled="true"
and disabled=true
, but these produce the same problem- I can't enable them again with JavaScript.
虽然这会禁用样式表,但 JavaScript(或至少我使用的方法)无法再次启用它。我还尝试了“禁用”属性的所有这些变体:disabled="disabled"
,disabled="true"
和disabled=true
,但是这些会产生相同的问题 - 我无法使用 JavaScript 再次启用它们。
In short, I need a way to enable/disable an external stylesheet using JavaScript, but have that stylesheet disabled by default but not relying on JavaScript.
简而言之,我需要一种使用 JavaScript 启用/禁用外部样式表的方法,但默认情况下禁用该样式表但不依赖于 JavaScript。
Any help would be greatly appreciated. Thanks.
任何帮助将不胜感激。谢谢。
N.B. This effect can be achieved by using two stylesheets, the second overwriting the first, therefore having no need for the "disabled" attribute. However, it's obviously preferable to only use a single stylesheet if possible, hence my question above.
注意这个效果可以通过使用两个样式表来实现,第二个覆盖第一个,因此不需要“禁用”属性。但是,如果可能的话,显然最好只使用一个样式表,因此我上面的问题。
回答by Scotty C.
You can use JavaScript's removeAttribute method for that.
为此,您可以使用 JavaScript 的 removeAttribute 方法。
<html>
<head>
<link id="first_style" rel="stylesheet" href="#" disabled />
<script type="text/javascript">
window.onload = function()
{
document.getElementById('first_style').removeAttribute('disabled');
}
</script>
</head>
<body>
<p>something</p>
</body>
</html>
回答by Alexis Pigeon
Why not turning the problem around : only load the CSS if JavaScript is enabled?
为什么不解决这个问题:如果启用了 JavaScript,只加载 CSS?
Taken from this example, you could use something like:
从这个例子中,你可以使用类似的东西:
var fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", filename)
回答by HankChizlJaw
I think in this case you may have to rely on JavaScript.
我认为在这种情况下你可能不得不依赖 JavaScript。
If you think more down the lines of serving up the style-sheet if needed rather than disabling by default you should nail it.
如果您在需要时更深入地考虑提供样式表而不是默认禁用,您应该确定它。