Can I add a custom attribute to an HTML tag?

后端 未结 17 2616
醉话见心
醉话见心 2020-11-22 08:06

Can I add a custom attribute to an HTML tag like the following?


17条回答
  •  自闭症患者
    2020-11-22 08:42

    Yes, you can do it!

    Having the next HTML tag:

    
    

    We can access their attributes with JavaScript:

    element.getAttribute('key'); // Getter
    element.setAttribute('key', 'value'); // Setter
    

    Element.setAttribute() put the attribute in the HTML tag if not exist. So, you dont need to declare it in the HTML code if you are going to set it with JavaScript.

    key: could be any name you desire for the attribute, while is not already used for the current tag. value: it's always a string containing what you need.

提交回复
热议问题