Difference between “[removed]” and “.value” in JS

前端 未结 3 1005
死守一世寂寞
死守一世寂寞 2020-11-27 22:03

I am confused on what is the difference between .innerHTML and .value in JavaScript. Here is my code:


Input string: &         


        
相关标签:
3条回答
  • 2020-11-27 22:20

    value refers to the value of an input element (or textearea)

    <input value="hello world">
    

    value would be "hello world" (or any value typed inside)


    innerHTML refers to the contents inside an HTML element.

    <div>
      <span class="hello">
         All tags and their children are include in innerHTML.
      </span>
      All this is part of innerHTML.
    </div>
    

    innerHTML of the div tag would be the string:

      '<span class="hello">
         All tags and their children are include in innerHTML.
      </span>
      All this is part of innerHTML.'
    
    0 讨论(0)
  • 2020-11-27 22:27

    .value gives you the currently-set value of a form element (input, select, textarea), whereas .innerHTML builds an HTML string based on the DOM nodes the element contains.

    0 讨论(0)
  • 2020-11-27 22:29

    The .innerHTML property refers to the literal HTML markup that is, once assigned, interpreted and incorporated into the DOM (Document Object Model) for the current document. On the other hand, the .value property simply refers to the content of typically an HTML input control, such as a textbox. Not every HTML element supports the input property, whereas most if not all support the innerHTML property.

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