Skip to content

Instantly share code, notes, and snippets.

@snaka
Created October 24, 2008 13:34
Show Gist options
  • Save snaka/19424 to your computer and use it in GitHub Desktop.
Save snaka/19424 to your computer and use it in GitHub Desktop.
/*
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