custom attribute works only with element.getAttribute(“attribute”) but not “element.attribute”

后端 未结 1 1110
日久生厌
日久生厌 2020-12-10 05:39

I have just noticed, that if I give a custom attribute to an html element, for example:



        
1条回答
  •  借酒劲吻你
    2020-12-10 06:14

    Only certain standard attributes are directly mapped to properties. This is not the defined behavior for non-standard (custom) attributes.

    The forward compatible way of using custom attributes is to prefix them with data-.

    
    

    Then they become available in HTML5 compliant browsers as:

    element.dataset.custom_attr
    

    But in legacy browsers, you'll still need to use .getAttribute().

    0 讨论(0)
提交回复
热议问题