欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

dom元素选择与xpath

程序员文章站 2024-03-24 19:16:46
...
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE var xpathResult = document.evaluate(
  xpathExpression,
  contextNode,
  namespaceResolver,
  resultType,
  result
);
xpathExpression is a string representing the XPath to be evaluated.
contextNode specifies the context node for the query (see the XPath specification). It's common to pass document as the context node.
namespaceResolver is a function that will be passed any namespace prefixes and should return a string representing the namespace URI associated with that prefix. It will be used to resolve prefixes within the XPath itself, so that they can be matched with the document. null is common for HTML documents or when no namespace prefixes are used.
resultType is an integer that corresponds to the type of result XPathResult to return. Use named constant properties, such as XPathResult.ANY_TYPE, of the XPathResult constructor, which correspond to integers from 0 to 9.
result is an existing XPathResult to use for the results. null is the most common and will create a new XPathResult

以下为验证结果
document.evaluate(’//div’, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
XPathResult {resultType: 7, invalidIteratorState: false, snapshotLength: 686}

document.evaluate(’//DiV’, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

XPathResult {resultType: 7, invalidIteratorState: false, snapshotLength: 686}
结果一致,大小写不影响

document.evaluate(’//DIV[@*]’, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
XPathResult {resultType: 7, invalidIteratorState: false, snapshotLength: 629}
[@*]为带属性的结果,无[@*]带属性和不带属性均能得到。

snapshotItem获取具体某项节点 Node

dom元素选择与xpath

dom元素选择与xpath
innerHTML 输出当前标签的文本内容,如果标签内有子标签,会连子标签本身和子标签内的文本内容一起输出

innerText 只输出当前标签内的文本内容,如果标签内有子标签,那么也只输出子标签内的文本内容

outerHTML 输出当前标签的本身和标签内的文本内容,如果有子标签,那么子标签本身和标签内的文本内容也将一起输出

outerText 只输出当前标签内的文本内容,如果标签内有子标签,那么也只输出子标签内的文本内容

相关标签: 学习之路