gpt4 book ai didi

javascript - document.evaluate 在 Chrome 中不起作用?

转载 作者:行者123 更新时间:2023-11-29 17:30:15 36 4
gpt4 key购买 nike

我有以下内容..

 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">

最佳答案

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

contextNode of type Node
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.

因此,您的上下文 必须是这些类的某个实例。我猜你可能根本没有设置这个变量。您还可以使用 null,上下文将成为您评估表达式的节点。

除此之外,请注意 //input[@class='form_field_as as-input'] 是一个绝对表达式,它会从任何上下文返回相同的结果(外部文档上下文不是当 "XPathEvaluator 是通过转换 Document" 获得时允许的。

关于javascript - document.evaluate 在 Chrome 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4960869/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com