为什么在IE 8中无法通过JavaScript设置标记的“ disabled”属性?
作者:互联网
我正在实现一个JavaScript函数,该函数可以启用/禁用网站的两个CSS文件:
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 );
}
如您所见,我启用或禁用链接标记.这适用于所有浏览器,但不适用于IE8.
有已知原因吗?
解决方法:
您正在切换链接元素的disabled
属性.正如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.
要从DOM中删除链接元素,请使用element.removeChild
.
只是一个愚蠢的例子:如果要删除页面上的第一个link元素,只需在浏览器的位置栏中输入以下JavaScript函数:
javascript:(function(){var a=document.getElementsByTagName('head')[0];a.removeChild(a.getElementsByTagName('link')[0])})();
(我只是在SO上尝试过,看起来很有趣:)
标签:html,javascript,internet-explorer-8 来源: https://codeday.me/bug/20191105/1997119.html