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

前端 未结 6 2121
挽巷
挽巷 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:42

    My guess (because you didn't specify the element type) is the element normally does not have a name attribute, so setting the DOM property like that won't work.

    For example, setting the name property on an input element will work. Setting it on a div will not.

    It will work, however, with setAttribute().

    jsFiddle.

提交回复
热议问题