向DOM Element对象添加属性有什么问题? [英] What's wrong with adding properties to DOM Element objects?

查看:293
本文介绍了向DOM Element对象添加属性有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一个直接的答案(我可以想到很多可能性,但我想知道真正的原因):

I've been looking for a straight answer for this (I can think of lots of possiblities, but I'd like to know the true reason):

jQuery提供了一种用于将数据与DOM Element对象相关联的.data()方法。什么使得这是必要的?直接向DOM元素对象添加属性(或方法)有问题吗?这是什么?

jQuery provides a .data() method for associating data with DOM Element objects. What makes this necessary? Is there a problem adding properties (or methods) directly to DOM Element Objects? What is it?

推荐答案


直接向DOM元素添加属性(或方法)有问题吗?对象?

Is there a problem adding properties (or methods) directly to DOM Element Objects?

潜在的

没有网页标准说你可以向DOM节点添加任意属性。它们是具有浏览器特定实现的主机对象,而不是根据ECMA-262的本机JavaScript对象,您可以执行所需的操作。其他主机对象将不允许您添加任意属性。

There is no web standard that says you can add arbitrary properties to DOM nodes. They are ‘host objects’ with browser-specific implementations, not ‘native JavaScript objects’ which according to ECMA-262 you can do what you like with. Other host objects will not allow you to add arbitrary properties.

在实际中,由于最早的浏览器允许您执行此操作,因此实际上是标准,你可以反正...除非你故意告诉IE不允许通过设置 document.expando = false 。你可能不会自己做,但是如果你正在编写一个脚本来部署在别的地方,那么它可能会关心你。

In reality since the earliest browsers did allow you to do it, it's a de facto standard that you can anyway... unless you deliberately tell IE to disallow it by setting document.expando= false. You probably wouldn't do that yourself, but if you're writing a script to be deployed elsewhere it might concern you.

任意属性有一个实际的问题因为您不会真正知道您所选择的任意名称在某些尚未测试的浏览器中或在未来版本的浏览器或标准中没有现有含义还不存在添加一个属性 element.sausage = true ,您不能确定任何空间和时间的任何浏览器都不会将其作为信号来使用令人兴奋的DOM香肠制作浏览器崩溃功能所以如果你添加任意属性,请确保给它一个不太可能的名字,例如 element._mylibraryname_sausage = true 。这也有助于防止与可能添加任意属性的其他脚本组件的命名空间冲突。

There is a practical problem with arbitrary-properties in that you don't really know that the arbitrary name you have chosen doesn't have an existing meaning in some browser you haven't tested yet, or in a future version of a browser or standard that doesn't exist yet. Add a property element.sausage= true, and you can't be sure that no browser anywhere in space and time will use that as a signal to engage the exciting DOM Sausage Make The Browser Crash feature. So if you do add an arbitrary property, make sure to give it an unlikely name, for example element._mylibraryname_sausage= true. This also helps prevent namespace conflicts with other script components that might add arbitrary properties.

在IE中还有一个问题,您添加的属性被错误地视为属性。如果您使用 innerHTML 序列化元素,则会在输出中获得意外的属性,例如。 < p _mylibraryname_sausage =true> 。如果您要将该HTML字符串分配给另一个元素,那么您将获得新元素中的一个属性,可能会使您的脚本混淆。

There is a further problem in IE in that properties you add are incorrectly treated as attributes. If you serialise the element with innerHTML you'll get an unexpected attribute in the output, eg. <p _mylibraryname_sausage="true">. Should you then assign that HTML string to another element, you'll get a property in the new element, potentially confusing your script.

(请注意,这仅适用于值是简单的类型;对象,数组和函数不会显示在序列化的HTML中,我希望jQuery知道这一点,因为它围绕它实现数据方法绝对可怕,导致错误,并减慢许多简单的DOM操作。)

(Note this only happens for properties whose values are simple types; Objects, Arrays and Functions do not show up in serialised HTML. I wish jQuery knew about this, because the way it works around it to implement the data method is absolutely terrible, results in bugs, and slows down many simple DOM operations.)

这篇关于向DOM Element对象添加属性有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆