在 quirksmode 中向元素添加类

发布于 2024-08-23 17:20:35 字数 881 浏览 5 评论 0原文

我尝试使用 javascript 在 Internet Explorer 8 中以怪异模式向 td 元素添加一个类。这似乎有效,因为当我查看源代码时我可以看到添加的类,但我的 css 不会影响它,所以视觉上没有任何变化。 我只是添加一个 html 类来更改背景颜色,但没有任何反应。 它在 IE 正常模式下运行时可以工作,但这不是一个选项,因为我无法更改站点并且它在怪异模式下运行。

编辑:

这是一个简单的示例:

<html>
<head>
<style>
    .style1 { background-color: #ff0000; }
    .style2 { background-color: #00ff00; }
</style>
</head>
<body>
<table id="table1">
    <tr>
        <td>some text</td>
        <td>goes on</td>
        <td>and on</td>
    </tr>
</table>
<script type="text/javascript">
    var tableElement = document.getElementById("table1");
    tableElement.setAttribute("class", "style1");
</script>
</body>
</html>

请注意,尽管正在添加该类(可以使用 IE 开发人员工具查看),但它在怪异模式下不起作用(使用 IE 8 进行测试)

I try to add a class to a td-element using javascript with the internet explorer 8 in quirks-mode. That seems to work, because I can see the added class when I view the source, but my css doens't affect it so nothing visually changes at all.
I simply add a html class to change the background-color but nothing happens.
It works when running in IEs normal mode, but that's not an option because I can't change the site and it's running in quirks-mode.

EDIT:

Here is a simple example:

<html>
<head>
<style>
    .style1 { background-color: #ff0000; }
    .style2 { background-color: #00ff00; }
</style>
</head>
<body>
<table id="table1">
    <tr>
        <td>some text</td>
        <td>goes on</td>
        <td>and on</td>
    </tr>
</table>
<script type="text/javascript">
    var tableElement = document.getElementById("table1");
    tableElement.setAttribute("class", "style1");
</script>
</body>
</html>

Note that it doesn't work in quirks-mode (tested with IE 8) although the class is getting added (can be viewed with IE developer tools)

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

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

发布评论

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

评论(1

如果没有 2024-08-30 17:20:35

Internet Explorer 7 及更低版本(以及模拟 7 时的 8)具有完全损坏的 setAttribute(和 getAttribute)实现。

实际上,它的工作原理如下:

HTMLElement.prototype.setAttribute = function (property, value) {
    this[property] = value;
}

当属性名称和属性名称不同时(例如,当属性名称是保留字(如类)或用于其他内容(如样式)时),这种情况会中断。

使用 foo.className = 'bar' 而不是 foo.setAttribute('class','bar')

Internet Explorer 7 and lower (and 8 when emulating 7) have a completely broken implementation of setAttribute (and getAttribute).

Effectively it works like this:

HTMLElement.prototype.setAttribute = function (property, value) {
    this[property] = value;
}

This breaks when the property name and attribute name are not the same (such as when the property name is a reserved word (like class) or used for something else (like style)).

Use foo.className = 'bar' instead of foo.setAttribute('class','bar')

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