向DOM对象添加自定义(您自己的)属性,好吗? [英] Adding custom ( your own ) properties to DOM objects, OK?
问题描述
<div id="theDiv"></div>
document.getElementByid('theDiv').myProperty='myValue';
if('undefined'!==typeof document.getElementById('theDiv').myProperty){
是否可以与跨浏览器兼容,以便为DOM对象提供自定义属性以供以后使用?
Is it ok and cross browser compatible to give DOM objects custom properties to be used later on?
如果仅需要将属性设置为<是否可以通过javascript使用strong>设置和检索代替setAttribute / getAttribute?
If the properties only need to be set and retrieved via javascript can this be used instead of setAttribute / getAttribute?
推荐答案
编辑2014/07 / 21
根据@blgt注释以及我阅读的所有其他内容,看来未来证明问题是您可能会遇到的唯一问题。
Edit 2014/07/21
According to @blgt comment and everything else I read, it seems that the future proof issue is the only problem you could encounter.
实际上,在上面进行jsperf显示它甚至比setAttribute / getAttribute
Actually, doing a jsperf on it shows it's even a bit faster than setAttribute / getAttribute
应该发表评论
也许这个答案可能对你有帮助ou: https://stackoverflow.com/a/3363501/3702797
Maybe this answer may help you : https://stackoverflow.com/a/3363501/3702797
或此问题
,尤其是此答案
这篇关于向DOM对象添加自定义(您自己的)属性,好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!