innerHTML 上的 __defineSetter__ 阻止其渲染

发布于 2024-09-03 00:34:16 字数 382 浏览 5 评论 0原文

我正在尝试为 HTML 元素创建一个监视方法,当属性更改时使用 __define[GS]etter__ 。当我设置值时,它反应得很好,但如果监听的属性是innerHTML,它会以某种方式无法呈现给定的字符串。所以基本上,当我向innerHTML 添加一些内容时,它不会显示。

我使用上一个问题中描述的监视方法: 监视 JavaScript 中的对象属性更改

我可能只是不听innerHTML更改,但我也想知道 __defineSetter__ 是否以某种方式阻止了设置值的原始处理。

谢谢!

i'm trying to create a watch method for HTML elements, using __define[GS]etter__ when a property is changed. It reacts just fine when i set the value, but if the property listened to, is innerHTML, it somehow fails to render the given string. So basically, when im adding something to innerHTML it doesn't show.

Im using the watch method described in this previous question:
Watch for object properties changes in JavaScript

I could of cause just not listen to innerHTML changes, but i'm also wondering if the __defineSetter__ somehow prevents original handling of setting the value.

Thanks!

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

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

发布评论

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

评论(3

剪不断理还乱 2024-09-10 00:34:16

该填充程序代码实际上并不直写:当您设置属性时,该值仅在监视包装器上被记住,而不会传递到底层对象。它是为纯 JavaScript 对象设计的,其属性没有副作用(例如在 innerHTML 的情况下更改 DOM)。

使其直写会很痛苦,因为无法直接调用原型的设置器。您必须暂时删除该属性,写入基础属性,然后将其放回原位。

然而,在我看来,这并不值得追求。 DOM 节点被允许作为“宿主对象”,因此不能保证任何本机 JavaScript 对象属性函数都可以在它们上工作。

(无论如何,向 Object 原型添加新成员通常被认为是一个非常糟糕的主意。)

我可能只是不听innerHTML更改

我认为这是最好的,是的。

That shim code doesn't actually write-through: when you set a property, the value is only remembered on the watch-wrapper and not passed down to the underlying object. It's designed for pure JavaScript objects whose properties have no side-effects (like changing the DOM, in innerHTML's case).

Making it write-through would be a pain since there's no way to directly call the prototype's setter. You'd have to temporarily remove the property, write to the underlying property, then put it back in place.

However it's not really worth pursuing IMO. DOM Nodes are permitted to be ‘host objects’, so there is no guarantee any of the native-JavaScript object-property functions will work on them at all.

(In any case, adding new members onto the Object prototype is generally considered a Really Bad Idea.)

I could of cause just not listen to innerHTML changes

I think that's best, yes.

傾旎 2024-09-10 00:34:16

好的更新。
我在 MSDN 上找到了这个页面,它正是我需要的:
http://msdn.microsoft.com/en-us /library/dd229916(VS.85).aspx

但是 Object.getOwnPropertyDescriptor 的处理方式显然只适用于 IE。真糟糕。任何想法将不胜感激。

Ok update.
I found this page on MSDN, which has exactly what i need:
http://msdn.microsoft.com/en-us/library/dd229916(VS.85).aspx

But the Object.getOwnPropertyDescriptor way of doing things apparently only works in IE. Bummer. Any ideas would be appreciated.

避讳 2024-09-10 00:34:16

我发现另一个网页看起来很有趣,但我无法完全让它执行我想要的操作:
http://www.refactory.org/s/recent/tag/accessors

我决定暂时找到一种解决方法,使用计时器来检查属性是否发生变化,并为支持该功能的浏览器提供属性更改事件网络。

但如果有人找到问题的解决方案,请发帖:)

I found another webpage with something that looked interesting, but i couldn't quite get it to do what i wanted:
http://www.refactory.org/s/recent/tag/accessors

I've decided to find a workaround for now, something with a timer that checks the attribute for changes and attribute change evnets for browsers that support that.

But if any one finds a solution for the question plz post :)

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