xpath4jquery.jsをjquery-1.3.jsで動かす

たまに、jQueryXPathセレクタ使えるというエントリーを見かけますが、CSSセレクタの間違いです。
例えば、idがhogeの要素を取得する場合

#hoge が CSS
id("hoge") が XPath

で、XPathを使いたいなぁと思っていたところ、
id:amachangさんがJavascript-XPath作ってた事を思い出し、使ってみた。XPath for jQueryと併用して、いざページ開いたら、
javascriptエラーが、"XPath for jQuery"の方でエラーになるみたいなので、ソース確認。


要は、関数の拡張なので、jQuery.extendとjQuery.fn.extendでいけるのでは?と素人考えで変更したらうまくいきました。

    // type is String or Number or Booleaen or Array or undefined
    // $(expr)
    // $(expr, context)
    // $(expr, type)
    // $(expr, ordered)
    // $(expr, context, type)
    // $(expr, context, ordered)
    // $(expr, type, ordered)
    // $(expr, context, type, ordered)
    jq.extend({
      x: function(expr, context, type, ordered) {
        var args = resolveArgs(expr, context, type, ordered);

        // 戻り値もjQueryオブジェクトにする
        return jq(xpath.apply(jq, args));
      }
    });

    // .xfind(expr)
    // .xfind(expr, ordered)
    fn.extend({
      xfind: function (expr, type, ordered) {
        if (typeof type == 'boolean') {
            ordered = type;
            type = undefined;
        }

        return this.pushStack(
          jq.map(this, function (node) {
              return xpath.call(jq, expr, node, type, ordered)
          })
        );
      }
    });


まだ、軽くしか動作確認していないので、使用は自己責任でお願いします。
むしろ、添削して。


XPathでも、iframeの中身までは探索できないのね。
$.x('//div', $('iframe')[0].contentDocument)
Node cannot be used in a document other than the one in which it was created" code: "4
本題がorz