Created
August 31, 2012 05:30
-
-
Save sofish/3549352 to your computer and use it in GitHub Desktop.
DOM: get the first child of a DOM element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var util = {}; | |
/* now: we use this one */ | |
util.first = function(element) { | |
if(!element) return; | |
return element[element.firstElementChild ? 'firstElementChild' : 'firstChild']; | |
} | |
/* former: */ | |
util.first = function(element) { | |
if(!element) return; | |
var first= element.firstChild; | |
while(first && first.nodeType !==1) first = first.nextSibling; | |
return first; | |
} | |
/* directly select */ | |
util.first = function(element, tag) { | |
if(!element) return; | |
tag = tag || '*'; | |
return element.querySelector ? element.querySelector(tag) : element.getElementsByTagName(tag)[0]; | |
} | |
/* children selector */ | |
util.first = function(element) { | |
return element && element.children[0]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment