简体   繁体   English

向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){

Is it ok and cross browser compatible to give DOM objects custom properties to be used later on? 可以为DOM对象提供自定义属性以供以后使用,并且可以与跨浏览器兼容吗?

If the properties only need to be set and retrieved via javascript can this be used instead of setAttribute / getAttribute? 如果仅需要通过javascript 设置和检索属性,可以使用它代替setAttribute / getAttribute吗?

Edit 2014/07/21 编辑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. 根据@blgt的评论和我阅读的所有内容,看来未来的证明问题是您可能遇到的唯一问题。

Actually, doing a jsperf on it shows it's even a bit faster than setAttribute / getAttribute 实际上,对它进行jsperf显示, 它甚至比setAttribute / getAttribute 一点


Should be comment 应该发表评论

Maybe this answer may help you : https://stackoverflow.com/a/3363501/3702797 也许这个答案可以帮助您: https : //stackoverflow.com/a/3363501/3702797

or this question 这个问题

and particularly this answer 特别是这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM