JavaScript knowledge points about value

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.