Javascript DOM: Setting custom DOM element properties

前端 未结 4 555
北荒
北荒 2020-12-05 18:35

Is it ok to set custom properties on DOM elements, and rely on them persisting?

For example, given

 
&l
4条回答
  •  我在风中等你
    2020-12-05 18:50

    No.

    Let's say 5 years ago you thought the property .contains was a good use-case for saying whether an element contained some special data.

    So you wrote document.getElementById("someId").contains = true

    Then you had checks like if (document.getElementByID("someId").contains)

    Today that breaks because Node.prototype.contains is a method.

    basically your code is not future safe.

提交回复
热议问题