为什么不设置“禁用”?的属性通过 JavaScript 标记可以在 IE 8 中工作吗?

发布于 2024-09-30 04:38:13 字数 729 浏览 2 评论 0原文

我正在实现一个 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。

有已知的原因吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏有森光若流苏 2024-10-07 04:38:13

您正在切换的 disabled 属性link 元素。正如 MDC 上所说,

[t]他的属性是非标准的并且
仅某些 Microsoft 浏览器使用
并且作者不得使用
要达到其效果,请使用其中之一
以下技术:

  • 如果disabled属性已直接添加到页面,则不要
    改为包含 元素;
  • 如果已通过脚本添加了 disabled 属性,请将其从
    通过脚本编写 DOM。

要从 DOM 中删除 link 元素,请使用 element .removeChild

只是一个愚蠢的例子:如果您想删除页面上的第一个 link 元素,只需在浏览器的地址栏中输入此 JavaScript 函数即可:(

javascript:(function(){var a=document.getElementsByTagName('head')[0];a.removeChild(a.getElementsByTagName('link')[0])})();

我刚刚尝试过,看起来很有趣:)

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 :)

暖伴 2024-10-07 04:38:13

如果您使用 removeAttribute('disabled') 而不是将其设置为 false 会怎样?

what if you use removeAttribute('disabled') instead of setting it to false?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文