jQuery 在 $(html_with_inline_styles) 上丢失样式

发布于 2024-10-16 13:10:03 字数 204 浏览 9 评论 0原文

在 Firefox 3.6(Firebug 控制台)中,执行

$('

').attr("style")

结果 中

"color: red;"

不可识别的样式规则为何会丢失?

In Firefox 3.6 (Firebug console), doing

$('<div style="-x-foo:bar;color:red;" />').attr("style")

results in

"color: red;"

Why are the non-recognized style rules lost?

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

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

发布评论

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

评论(3

沫尐诺 2024-10-23 13:10:04

jQuery 将创建一个新的 DOM 元素。 Firefox 将解析该元素的 style 属性并忽略任何它不理解的内容。

值得注意的是,Chrome 不会删除这样的属性。

尝试此链接在不同浏览器中测试此行为 - 感谢ILMV

jQuery will create a new DOM element. Firefox will parse the style attribute of that element and ignore anything it doesn't understand.

It's worth noting that Chrome doesn't remove attributes like this.

Try this link to test this behaviour in different browsers - thanks ILMV!

五里雾 2024-10-23 13:10:04

对于浏览器应如何处理无法识别的规则,没有定义的操作。有些浏览器可能会将它们添加到 DOM 但会忽略它们,其他浏览器可能会完全删除它们。

Firefox 是放弃未知规则的浏览器之一。

您在其他浏览器中可能会得到不同的结果。关键是它是未定义的,因此很难预测会发生什么,即使在同一浏览器的不同版本之间也是如此。

我想真正的问题是:您试图指定的无法识别的规则是什么?你显然是想在这里玩一些聪明的把戏,我怀疑你所得到的问题答案是否就是你为了让你的聪明把戏发挥作用而寻找的答案。也许如果您描述了您试图通过这个未被认可的规则实现什么目标,我们可以帮助您找到实现它的方法,特别是因为您当前的计划似乎不太可能成功。

There is no defined action for what a browser should do with unrecognised rules. Some browsers may add them to the DOM but ignore them, others may drop them entirely.

Firefox is one of those that drops unknown rules.

You may have different results in other browsers. The point is that it's undefined, so will be hard to predict what's going to happen, even between versions of the same browser.

I guess the real question is: What is the unrecognised rule you're trying to specify? You're clearly trying to do some kind of clever trick here, and I doubt that the answers you've been given to the question will be the answer you were looking in order for your clever trick to work. Perhaps if you described what it was you were trying to achieve with this unrecognised rule, we could help you find a way of achieving it, especially since your current plan doesn't seem likely to pan out.

幼儿园老大 2024-10-23 13:10:04

浏览器在创建节点并设置属性值时会丢弃它无法识别的属性。

当您获得浏览器在内存中保存的内容的序列化时,它们就不再存在。

The browser discards properties it does not recognise when it creates the node and sets the attribute values.

When you get a serialisation of what the browser holds in memory, they no longer exist.

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