-
-
Save PeterRao/9804812 to your computer and use it in GitHub Desktop.
This file contains 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
// 大家写在评论中吧,代码高亮可以这样写: | |
// ```js | |
// your code | |
// ``` | |
// update: Fri Aug 31 08:39:21 | |
// copyright: https://gist.github.com/3549352 | |
// 加个性能测试:http://jsperf.com/get-dom-s-first-element | |
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; | |
} | |
// update: add 2 more | |
/* directly select | |
*cc @qq286735628 | |
*/ | |
util.first = function(element, tag) { | |
if(!element) return; | |
tag = tag || '*'; | |
return element.querySelector ? element.querySelector(tag) : element.getElementsByTagName(tag)[0]; | |
} | |
/* children selector | |
* cc @nomospace @qq286735628 | |
*/ | |
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