[removed] setAttribute() v.s. element.attribute = value to set “name” attribute

前端 未结 6 2158
挽巷
挽巷 2020-12-03 11:07

So I\'m learning to manipulate the DOM and I noticed one interesting thing:

Let\'s say I want to set the name attribute of an element by using the \".\"

6条回答
  •  臣服心动
    2020-12-03 11:41

    when you use, element.name, you are accessing the property/creating a property named "name" and setting its value.

    but,

    while using, element.setAttribute('name','someName'), you are actually setting the attribute 'name'.

    IE8 and below treats the property and attribute as same, the bug has been fixed in IE9 and above.
    Safari, FireFox, Chrome treat property and attribute differently.

    However, you can always create a new property of your choice if you wish to do so.

提交回复
热议问题