Skip to content

Instantly share code, notes, and snippets.

@PeterRao
Forked from sofish/first.js
Created March 27, 2014 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeterRao/9804812 to your computer and use it in GitHub Desktop.
Save PeterRao/9804812 to your computer and use it in GitHub Desktop.
// 大家写在评论中吧,代码高亮可以这样写:
// ```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