Compatible firstchild,lastchild,nextsibling,previoussibling notation

Source: Internet
Author: User

Firstchild,lastchild,nextsibling,previoussibling is supported under IE

But under FF, because it will be the space between the labels as a text node, so in order to accurately find the corresponding elements, will be used

Firstelementchild,

Lastelementchild,

Nextelementsibling,

Previouselementsibling

Compatible notation is like this

var Ofirst = oparent.firstelementchild| | Oparent.firstchild
You can write it like that.
var ofirst = oparent.children[0];

var olast = oparent.lastelementchild| | Oparent.lastchild
You can write it like that.
var olast = oparent.children[oparent.children.length-1];

var onext = Obj.nextelementsibling| | Obj.nextsibling

var opre = Obj.previouselementsibling| | Obj.previoussibling

<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>Untitled Document</title><Script>window.onload= function() {    varOul=document.getElementById ('UL1'); /*element. LastChild | | element. Lastelementchild the first child node element. LastChild | | element. Lastelementchild the last child node element. Nextsi Bling | | Element. Nextelementsibling the next sibling node element. previoussibling | | element. previouselementsibling Previous Sibling node*/    varOfirst=Oul.firstelementchild||Oul.firstchild; //var ofirst = oul.children[0];OFirst.style.background= 'Red'; varOlast=Oul.lastelementchild||Oul.lastchild; //var olast = oul.children[oul.children.length-1];OLast.style.background= 'Yellow'; varOnext=ofirst.nextelementsibling||ofirst.nextsibling; ONext.style.background= 'Blue'; varOprev=olast.previouselementsibling||olast.previoussibling; OPrev.style.background= 'Orange'; }</Script></Head><Body><ulID= "UL1">        <Li>11111</Li>        <Li>22222</Li>        <Li>33333</Li>        <Li>44444</Li>    </ul></Body></HTML>

Compatible firstchild,lastchild,nextsibling,previoussibling notation

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.