2011-02-10 20 views
2

私は評価してる式が入力から..ですdocument.evaluateはChromeで動作していませんか?

var result = doc.evaluate("//input[@class=\"form_field_as as-input\"]", 
          context, 
          null, 
          XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, 
          null); 
    for(var i = 0; i < result.snapshotLength; i++) { 
    a[i] = result.snapshotItem(i); 
} 
return a; 

を、次のを持っています。コードはFireFoxで正常に動作しますが、Chromeでテストすると何も返されません。私は間違って何をしていますか?

私は評価してるの入力が..です

<input type="text" id="sharees" class="form_field_as"> 
+0

コードでChromeのjavascriptコンソールをチェックしているときに、「doc」が定義されていないというエラーが表示されます。 – Stephen

+0

'doc'はちょうど' document'です – Skizit

+0

@Skizit:明らかにそうではありませんか? – drudge

答えて

5

http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathEvaluator-evaluate

contextNodeof typeNode
The context is context node for the evaluation of this XPath expression. If the XPathEvaluator was obtained by casting the Document then this must be owned by the same document and must be a Document , Element , Attribute , Text , CDATASection , Comment , ProcessingInstruction , or XPathNamespace node. If the context node is a Text or a CDATASection , then the context is interpreted as the whole logical text node as seen by XPath, unless the node is empty in which case it may not serve as the XPath context.

からだから、あなたのcontextは、これらのクラスのいくつかのインスタンスでなければなりません。おそらくこの変数を設定していないと思います。 nullも使用できます。コンテキストは、式を評価しているノードになります。

はそれに加えて、//input[@class='form_field_as as-input']絶対表現であることに注意しないと、それは、任意のコンテキストから同じ結果を返す(XPathEvaluatorDocumentをキャスティングすることによって得られた」とき外部文書コンテキストが許可されていません)。

+0

Cool。私はリソースとしてhttps://developer.mozilla.org/en/introduction_to_using_xpath_in_javascriptで似たような回答を投稿していました。このページの例はChromeで正常に動作します。 – Stephen

関連する問題