创建元素有哪些不同的方式

发布于 2024-12-28 05:31:36 字数 718 浏览 2 评论 0原文

假设我想覆盖所有表单(当前和未来)上的 form.elements

动机

这个关于 polyfilling RadioNodeList 的问题可以通过拦截所有 < code>form 元素并为其上的 elements 属性手动编写自己的 getter。

我该如何拦截?

除了下面的方法之外,还有哪些其他方法可以创建我需要拦截的(表单)元素

  • document.createElement
  • document.createElementNS
  • Node.importNode
  • Node.cloneNode
  • DOMImplementation.createDocument (也许,取决于覆盖 Document.prototype.createElement/NS 的效果)

其他还有哪些缺点?

另一个问题,忽略性能影响有什么好的理由不拦截节点创建?

Say I want to overwrite form.elements on all forms (current and future).

Motivation

This question on polyfilling RadioNodeList could be solved by intercepting all form elements and manually writing an own getter for the elements property on them.

How do I intercept?

Apart from the below what other ways are there of creating (form) elements that I need to intercept

  • document.createElement
  • document.createElementNS
  • Node.importNode
  • Node.cloneNode
  • DOMImplementation.createDocument (Maybe, depends how effective overwriting Document.prototype.createElement/NS is)

What are the other downsides?

Seperate question, ignoring the performance hits what good reasons are there for not intercepting node creation?

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

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

发布评论

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

评论(1

我的影子我的梦 2025-01-04 05:31:36

表单元素的拦截可以通过一个函数来完成,该函数通过搜索表单的标签名称来解析节点,如果特定的事件或属性没有像您那样设置,那么您必须设置它,否则它是一个已经拦截的表单。

第二种选择是重写 Elementdocument 上的 createElement ,以便新元素由上述函数解析。

不好的部分是,对于较旧的 IE 版本,您必须编写一个单独的 HTC 文件才能使 Element 的行为符合您的意愿。

希望这会有所帮助,即使它有很多信息并且没有代码(这是一个非常讨厌的代码)。

编辑:

换句话说,您可以创建一个用于跨浏览器使用的重写 DOM 类(或者至少这是我过去 4 个月所做的事情),您可以在其中采用所有特殊行为并出于特殊原因对待它们。

The interception of the form elements can be done with a function that parses the nodes by tag name searching for form if a specific event or attribute is not set like you have than you would have to set it else it is an already intercepted form.

The second alternative is to override the createElement on Element and on document for the new elements to come to be parsed by the above mentioned function.

The bad part is that for older IE versions you would have to write a separate HTC file to make the Element's behavior as you wish.

Hope this helps, even if it is a lot of info and no code ( it is a pretty nasty code ).

EDIT:

In other words you could create a Overridden DOM class for Cross-Browser usage ( or at least this is what i am doing for the las 4 months ), where you take all the special behaviors and treat them for special reasons.

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