Skip to content

Instantly share code, notes, and snippets.

@cuixiping
Created August 23, 2012 14:36
Show Gist options
  • Save cuixiping/3437229 to your computer and use it in GitHub Desktop.
Save cuixiping/3437229 to your computer and use it in GitHub Desktop.
设 A = $("#id a"),B = $("#id .c a"),求 A - B。
// oldj:设 A = $("#id a"),B = $("#id .c a"),求 A - B。要求:
// 1、不能用 jQuery 等框架;
// 2、兼容 IE6 在内的各大浏览器;
// 3、尽可能高效;
// 4、尽可能简短。
function getWanted() {
var root = document.getElementById('id');
var aa = Array.prototype.slice.call(root.getElementsByTagName('a'),0);
for(var i = aa.length-1; i >= 0; i--) {
if (check(aa[i].parentNode)) {
aa.splice(i,1);
}
}
return aa;
}
function check(e){
while(e && e.id!='id'){
if(hasClass(e, 'c')){
return true;
}
e=e.parentNode;
}
return false;
}
function hasClass(node, className) {
return (' ' + node.className + ' ').indexOf(' ' + className + ' ') > -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment