Created
July 25, 2012 15:36
-
-
Save lifesinger/3176815 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
// oldj:设 A = $("#id a"),B = $("#id .c a"),求 A - B。要求: | |
// 1、不能用 jQuery 等框架; | |
// 2、兼容 IE6 在内的各大浏览器; | |
// 3、尽可能高效; | |
// 4、尽可能简短。 | |
function getWanted() { | |
var root = document.getElementById('id') | |
var all = root.getElementsByTagName('*') | |
var slice = Array.prototype.slice | |
var ret = [] | |
for(var i = 0, len = all.length; i < len; i++) { | |
var node = all[i] | |
if (!hasClass(node, 'c')) { | |
ret = ret.concat(slice.call(node.getElementsByTagName('a'))) | |
} | |
} | |
// 还需要去重,不过不想折腾了,呵呵 | |
return ret | |
} | |
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
上面的格式弄错了,重新再弄了一下。
lifesinger的主要代码是这样:
这个逻辑显然是错的,不信试试:
正确的应该是: