Value in JavaScript is both a property and a variable
"1" The following is a common situation, will pop up "test content"
<input type= "button" value = "Test Content" onclick = "alert (value)" >
"2" thought, in this case value cannot find, along the scope chain should go to the document, should pop up "123", but the situation is popup empty
<script> var value = 123; </script><input type= "button" onclick = "alert (value)" >
"3" value is really not found? is to find. Under the Debugging tool, the properties of this are viewed with a ' value: '. Its value is empty.
<input type= "button" onclick = "Console.log (this)" >
"4" So value as the attribute of input has been present, there is no found case, assigned value is the assigned value, no assignment value is null
"5" Look at a case extension, value camouflage brother Val. Val first found on the input object, not found, along the scope chain on the Document object to find, found the popup 123, here val=123 is written in front, but back, can be accessed, because the onclick is only event binding, When the event really happens, the page has already parsed the code behind var val = 123. So there is no error.
<script>var val = 123; </script><input type= "button" onclick = "Console.log (val)" >
<script>
var val = 123;
</script>
"6" is that so? But in fact, the statement put in the back is not reliable, if there are other <script> code, due to network reasons can not access, because of the blocking effect of <script>, will block the following code, will error
<input type= "button" onclick = "alert (val)" ><script src= "Http://www.qq.com/test.js" ></script>< Script>var val=123; </script>
"7" The last expansion. If it is a form element, its scope chain is this->this.form->document. Look for the username attribute from <input type= "button" > object, and find No. Then find its parent form,form username can find <input type= "text" > element (form element can be accessed directly by name value) and then find its value 123 Popup
<form action= "#" > <input type= "text" name= "username" value= "123" > <input type= "button" value= "BTN" onclick= "alert (username.value)" ></form>
Reprint Source: The Blue ideal of small matches
JavaScript knowledge points about value