3

有谁知道为什么 IE8 在加载页面时会从标题标签中删除 data- 属性?我没有在任何其他浏览器中看到这种行为。

如果在 IE8 中加载以下 html 并在 Dev 工具中检查 DOM,则 datadata-title-attribute属性已被删除。

<!DOCTYPE html>
<html>
    <head>
        <title data-title-attribute="Damn you IE8!">Title</title>
    </head>
    <body data-body-attribute="This works!"></body>
</html>
4

1 回答 1

2

稍加修改后,看起来 IE8<title>在呈现时从标记中删除了所有非 HTML4 属性。

作为一种解决方法,一旦将任何属性<title>呈现到 DOM 中,您就可以设置/添加任何属性。所以以下在我的测试中确实有效:

<!DOCTYPE html>
<html>
    <head>
        <title id="title" data-title-attribute="Damn you IE8!">Title</title>
        <script>document.getElementById('title').setAttribute('data-title-attribute', 'Damn you IE8!');</script>
    </head>
    <body data-body-attribute="This works!"></body>
</html>

这将导致您的data-*属性被添加到标题标签并可以通过 Javascript 访问。

至于为什么这样做,老实说,我认为您在 IE8 中发现了一个错误,因为它确实支持data-*几乎所有其他位置的属性。

于 2013-10-11T16:00:31.257 回答