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

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

    (Attempting to explain part of the above post a better, separately, since it is already went into -ve rating, and belief in that post will be less. Help improve this further if not better.)

    *** The property

    When you use, element.name, you are accessing an existing property named "name" or setting its value.

    Example 1:
    var div1 = document.getElementById("div1"); 
    div1.textContent = "2";
    

    *** The attribute

    but, while using, element.setAttribute('name','someName'), you are actually setting the attribute named 'name'. This attribute can be an existing property OR a custom one we want:

    Example 2:
    var h1 = document.getElementById("H1"); 
    h1.setAttribute("class", "democlass");
    
    Example 3:
    var d = document.getElementById("d1"); 
    d.setAttribute("name1", "value1");
    

提交回复
热议问题