[JavaScript] Dom Basics

Source: Internet
Author: User

Dom is the document.

Compatibility conditions

-ie6~8 10%

-chrome 60%

-ff 99%

DOM node

Child nodes

-childnodes contains text nodes and ELEMENT nodes//ie6-8

ABCDE text node <SPAN>ABCD element node </span>

-nodetype return node type

NodeType = = 3 Text node

NodeType = = 1 element node

<script>function() {    var oul=document.getelementbyid (' ul1 ');      for (var i=0;i<ali.length;i++) {        if(oul.childnodes[i].nodestype = = 1) {           = ' red ';}}    } </script><body><ul id= "UL1" >    <li></li>    <li></li></ul> </body>

-children includes only elements, not text

Parent node

-parentnode

<script>window.onload=function(){  varAa=document.getelementsbytagnaame (' a ');  for(vari=0;i<aa.length;i++) {Aa[i].onclick=function(){         This. ParentNode.style.display = ' None '; }    }}</script><body><ul> <li>111<a href= "JavaScript" > Hide </a></li> <li> 222<a href= "JavaScript" > Hide </a></li> <li>333<a href= "JavaScript" > Hide </a></li > <li>444<a href= "javascript" > Hide </a></li> <li>555<a href= "JavaScript" > Hidden </a></li></ul></body>

-offsetperent get the actual position of the element on the page

-firstchild Gets the first child node//ie6-8

-firstelementchild//chrome, FF

Compatibility issue Resolution

<script>function() {   var oul = document.getElementById (' ul1 ');     if (oul.firstelementchild) {        = ' red ';    }     Else (oul.firstchild) {       = ' red ';    }} </script><body><ul id = "UL1" >    <li></li>    <li></li>    < li></li>    <li></li>    <li></li><ul></body>

Element Property Manipulation

-getattribute (name) get

-setattribute (name, value) settings

-removeattribue (name) Delete

Get elements with classname

<script>functionGetbyclass (oparent,sclass) {varAele = Oparent.getelementsbytagname (' * ')); varAresult = [];  for(vari=0;i<aele.length;i++){        if(Aele[i].classname = =Sclass)         {Aresult.push (aele[i]); }    }    returnAresult;} Window.onload=function(){    varOul = document.getElementById (' ul1 ')); varAbox = Getbyclass (Oul, ' box ');  for(vari=0;i<abox.length;i++) {Abox[i].style.background= ' Red '; }}</script>

[JavaScript] Dom Basics

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.