为什么不设置< link>的“已禁用”属性?通过JavaScript标记在IE 8中工作? [英] Why doesn’t setting the “disabled” property of a <link> tag via JavaScript work in IE 8?
问题描述
function switch_style()
{
var i,link_tag; ((link_tag [i] .rel.indexOf
for(i = 0,link_tag = document.getElementsByTagName(link); i< link_tag.length; i ++)
{
(stylesheet)!= -1)&& link_tag [i] .title)
{
if(link_tag [i] .title ==normal)
{
link_tag [i] .disabled = true;
}
else if(link_tag [i] .title ==contrast)
{
link_tag [i] .disabled = false;
}
}
}
set_cookie(style_cookie_name,contrast,style_cookie_duration);
$ / code>
正如您所看到的,我启用或禁用链接标记。这可以在所有浏览器中使用,但不能在IE8中使用。
是否有一个已知的原因?
您正在切换 disabled
属性链接
元素。正如在MDC上所说,
[t]他的属性是非标准的,
仅被一些微软浏览器使用
和不得使用作者。
要达到其效果,请使用以下技巧之一
- 如果
禁用
属性直接添加到页面中,请勿
包含< link>
元素;
- 如果通过脚本添加了
disabled
属性,请通过脚本将它从
中移除。
要从DOM中移除链接
元素,请使用 element.removeChild
。
只是一个愚蠢的例子:如果你想删除页面上的第一个链接
元素,只需在浏览器的位置输入这个JavaScript函数bar:
javascript:(function(){var a = document.getElementsByTagName('head')[0]; a。 removeChild之(a.getElementsByTagName( '链接')[0])})();
(我刚刚尝试过,看起来很有趣:)
I am implementing a JavaScript function that enables/disables two CSS file of a website:
function switch_style ()
{
var i, link_tag ;
for (i = 0, link_tag = document.getElementsByTagName("link"); i < link_tag.length ; i++ )
{
if ((link_tag[i].rel.indexOf( "stylesheet" ) != -1) && link_tag[i].title)
{
if(link_tag[i].title == "normal")
{
link_tag[i].disabled = true;
}
else if (link_tag[i].title == "contrast")
{
link_tag[i].disabled = false;
}
}
}
set_cookie( style_cookie_name, "contrast", style_cookie_duration );
}
As you can see, I enable or disable a link tag. This works in all browsers but not in IE8.
Is there a known reason?
You're toggling the disabled
property of link
elements. As said on MDC,
[t]his attribute is non-standard and only used by some Microsoft browsers and must not be used by authors. To achieve its effect, use one of the following techniques:
- If the
disabled
attribute has been added directly to the page, do not include the<link>
element instead;- If the
disabled
attribute has been added via scripting, remove it from the DOM via scripting.
To remove a link
element from the DOM, use element.removeChild
.
Just a stupid example: if you want to remove the first link
element on a page, just enter this JavaScript function in your browser's location bar:
javascript:(function(){var a=document.getElementsByTagName('head')[0];a.removeChild(a.getElementsByTagName('link')[0])})();
(I just tried it on SO, looked funny :)
这篇关于为什么不设置< link>的“已禁用”属性?通过JavaScript标记在IE 8中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!