向DOM对象添加自定义(您自己的)属性

Adding custom ( your own ) properties to DOM objects, OK?

本文关键字:自己的 属性 自定义 DOM 对象 添加      更新时间:2023-09-26
<div id="theDiv"></div>
document.getElementByid('theDiv').myProperty='myValue';
if('undefined'!==typeof document.getElementById('theDiv').myProperty){

是否可以跨浏览器兼容给DOM对象自定义属性以后使用?

如果属性只需要设置和检索通过javascript可以使用这而不是setAttribute/getAttribute?

编辑2014/07/21

根据@blgt评论和我读到的其他所有东西,似乎未来证明问题是你可能遇到的唯一问题。

实际上,对它进行jsperf显示它甚至比setAttribute/getAttribute

快一点

应该是注释

也许这个答案对你有帮助:https://stackoverflow.com/a/3363501/3702797

或者这个问题

尤其是这个答案