Posted by をかもと at 2007年11月14日 水曜日
id:amachang がリリースした JavaScript-XPath が、かなり良さげ。
JavaScript-XPath の検索結果を jQuery オブジェクトにして、利用できると便利そうなので、以下のようなコードを書いてみた。
(function(jQuery){
jQuery.xpath = function(selector, context){
context = context || jQuery(document);
var r, res=[];
context.each(function() {
r = document.evaluate(selector, this, null, 7, null);
for(var node_no=0; node_no<r .snapshotLength; node_no++){
res.push(r.snapshotItem(node_no));
}
});
return new jQuery(res);
};
jQuery.extend(jQuery.prototype, {xpath: function(selector){return jQuery.xpath(selector, this);}});
})(jQuery);
var $x = jQuery.xpath;
これで、jQueryオブジェクト.xpath() または $x() を介して JavaScript-XPath 検索結果を jQuery オブジェクトとして利用することができる。
使い方は jQueryオブジェクト.xpath("XPath 文字列") または $x("XPath 文字列", jQueryオブジェクト)
$x() の第2引数は省略可能で、検索範囲を限定したい場合にセットしてください。
...続きを読む
