Created
October 24, 2008 13:34
-
-
Save snaka/19424 to your computer and use it in GitHub Desktop.
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
/* | |
XPath utility | |
usage: var results= x$('//div'); | |
x$('//div').each(function() { | |
... | |
}); | |
*/ | |
function $x(exp, obj) { | |
var target = obj || document; | |
var r = document.evaluate(exp, target, null, 7, null); | |
// 配列に"each"を追加 これはちょっと危ないか? | |
if (!("each" in Array.prototype)) { | |
Array.prototype.each = function(fnc) { | |
for(var i = 0; i < this.length; i++) { | |
fnc(this[i]); | |
} | |
} | |
} | |
// 独自拡張版配列に詰めて返す | |
var result = new Array(); | |
for (var i = 0; i < r.snapshotLength; i++ ) { | |
result.push(r.snapshotItem(i)); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment