dom元素选择与xpath

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

在这里插入图片描述

在这里插入图片描述
innerHTML 输出当前标签的文本内容,如果标签内有子标签,会连子标签本身和子标签内的文本内容一起输出

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

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

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

猜你喜欢

转载自blog.csdn.net/u012787710/article/details/103630547
今日推荐