JS Learning 12-"JS DOM programming Art" notes

Source: Internet
Author: User

Learn the next "JS DOM programming Art", to do a study summary:
1. Enclose single quotation marks in a string:
Suggestions:
Enclose a single quotation mark in a string, enclose the string in double quotation marks

1 var s1= "It ' s My Doy";

Enclose double quotation marks in a string, enclose the string in single quotation marks
1 var s2= ' He said "hi!" ';
Other uses \ to escape

2.== and = = =
= = = Strict comparison. Not only compare values, but also compare types
= =: Non-strict comparison, conversion type consistent comparison

Three types of objects in 3.JS languages
3.1 Built- in Objects : JavaScript-supplied objects Math Array
3.2 Host Object : Browser object. such as Document,window
3.3 User Custom objects:..

4. Get the objects in the document
1 document.getElementById ("P1");

1 document.getelementbytagname ("input");

1 document.getelementbyclassname ("classname1 classname2"); // Note: The name order is not important at this time, can be disorderly

5. Get an object property
eg

var html=document.getelementsbytagname ("html");//html array

Html[0].getattribute ("title");//The result is null at this time, which means no value

html[0].title;//The result is ""; there may be a compatibility issue with the old version of the Web document processing mode

6.event event returns false to cause the default behavior to not be triggered
eg

1 <  href= "www.baidu.com"  onclick= "return false">onclick </ a >//Click the link at this time, jump to the Baidu page of the event will not trigger

7. Get the child nodes of a node  

var child=document.getelementsbytagname ("body") [0].childnodes;

Child.length;

Child[0].nodetype; // returns a number with a result of 1 2 3. Where: 1-element node, 2-Attribute node, 3-text node

8. Get the value of a text node
Element.nodevalue;
eg

1 <Body>2 ********test nodetype,nodevalue*******3 <P>Hello Jermy</P>4 <Scripttype= "Text/javascript">5 functiontag (tagname) {6 returndocument.getElementsByTagName (tagname) [0];7 }8 varP1=Tag ("P");9 varT1=P1.firstchild;//equivalent to p1.childnodes[0]Ten document.write ("p1.nodetype="+P1.nodetype+";p 1.nodevalue="+P1.nodevalue+"<br/>"); One document.write ("t1.nodetype="+T1.nodetype+"; t1.nodevalue="+t1.nodevalue); A </Script> - </Body>

9. Pseudo-Protocol (note: This method is not recommended)
<a href= "Javascript:prop ()" ></a>//here click a link, in support of the pseudo-protocol browser run directly, do not support the pseudo-protocol browser will error. Pseudo-protocol support but the user-disabled JS browser will do nothing.

Website design Focus: smooth degradation


One. Line wrap String

1 document.getElementById ("Demo"). InnerHTML = "Hello \ 2 dolly!";

12. Self-booting:

1 (function () { 2 document.getElementById ("Demo"). InnerHTML = "hello! I called myself "; 3 } ());

Or:

1 (function () { 2 document.getElementById ("Demo"). InnerHTML = "hello! I called myself "; 3 }) ();

13. Examples of closures

1 <!DOCTYPE HTML>2 <HTML>3 <Body>4 5 <P>Counting with a local variable.</P>6 7 <Buttontype= "button"onclick= "myFunction ()">count!</Button>8 9 <PID= "Demo">0</P>Ten  One <Script> A varAdd= (function () { - varcounter= 0; - return function () {returncounter+= 1;} the })(); -  - functionmyFunction () { - document.getElementById ("Demo"). InnerHTML=Add (); + } - </Script> +  A </Body> at </HTML>

Closure results See picture

JS Learning 12-"JS DOM programming Art" notes

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.