Document.evaluate()
åºçº¿
广æ³å¯ç¨
èª 2015å¹´7æ èµ·ï¼æ¤ç¹æ§å·²å¨ä¸»æµæµè§å¨ä¸å¾å°æ¯æï¼å¯å¨å¤§å¤æ°è®¾å¤åæµè§å¨çæ¬ä¸æ£å¸¸ä½¿ç¨ã
æ ¹æ®ä¼ å
¥ç XPath 表达å¼ä»¥åå
¶ä»åæ°ï¼è¿åä¸ä¸ª XPathResult 对象ã
è¯æ³
evaluate(xpathExpression, contextNode, namespaceResolver, resultType, result)
xpathExpression表示è¦è®¡ç®ç Xpath å符串ãcontextNodeè¡¨ç¤ºæ¬æ¬¡æ¥è¯¢çä¸ä¸æèç¹ï¼åç § XPath è§è http://www.w3.org/TR/xpathï¼ãé常ä¼ä½¿ç¨documentãnamespaceResolveræ¯å½æ°ãä¼ å ¥å空é´åç¼ï¼è¿åè·æ¤åç¼ç¸å ³çåç©ºé´ URIï¼å符串ï¼ãéå¸¸ç¨æ¥è§£æ Xpath å çåç¼ï¼ä»¥ä¾¿å¯¹ææ¡£è¿è¡å¹é ãHTML ææ¡£æè ä¸ä½¿ç¨å空é´åç¼çææ¡£ï¼éå¸¸ä¼ å ¥nullãresultTypeæ¯æ´æ°ãæå®æè¿åçXPathResultçç±»åï¼å¸¸ä½¿ç¨ named constant propertiesï¼å¦XPathResult.ANY_TYPEï¼èå´ 0 å° 9ï¼è§ä¸è¡¨ãresult为XPathResultåï¼ç¨ä»¥å卿¥è¯¢ç»æãéå¸¸ä¼ å ¥nullï¼æ¤æ¶å°å建æ°çXPathResult对象ã
示ä¾
var headings = document.evaluate(
"/html/body//h2",
document,
null,
XPathResult.ANY_TYPE,
null,
);
/* å¨ document 䏿¥æ¾ææç h2 å
ç´ ã
* ç»æå¯è½æ¯æ åºèç¹è¿ä»£å¨ã */
var thisHeading = headings.iterateNext();
var alertText = "Level 2 headings in this document are:\n";
while (thisHeading) {
alertText += thisHeading.textContent + "\n";
thisHeading = headings.iterateNext();
}
alert(alertText); // æ¾ç¤ºææ h2 èç¹çææ¬
注æï¼å¨ä¸è¿°ä¾åä¸ï¼æå¥½åæ´åé¿ç XPathï¼è䏿¯å¸¸ç¨çç®åï¼æ¯å¦ //h2ãé常ï¼åä¸è¿°ä¾åæç¤ºï¼æ´å
·ä½ç XPath éæ©å¨ä¼å¾å°æ¾èçæ§è½æåï¼ç¹å«æ¯å¨éå¸¸å¤§çææ¡£ä¸ãè¿æ¯å 为æ¥è¯¢è®¡ç®ä¸ä¼å°æ¶é´æµªè´¹å¨æ¥çä¸éè¦çèç¹ä¸ãä½¿ç¨ // é叏徿
¢ï¼è¿æ¯å 为å®è¦ä»æ ¹èç¹åææåèç¹ä¸æ¥æ¾ææå¯è½å¹é
çèç¹ã
éè¿è°¨æ
使ç¨ä¸ä¸æåæ°è½å¾å°è¿ä¸æ¥çä¼åãæ¯å¦ï¼å¦æä½ ç¥éä½ è¦æ¥æ¾çå
å®¹å¨ body æ ç¾çæå¤ï¼ä½ å¯ä»¥è¿æ ·åï¼
document.evaluate(".//h2", document.body, null, XPathResult.ANY_TYPE, null);
注æä¸é¢ç document.body å·²ç»æ¿ä»£äº document ä½ä¸ºä¸ä¸æï¼æä»¥ XPath ä» body å
ç´ å¼å§æ¥æ¾ãï¼å¨è¿ä¸ªä¾åä¸ï¼"." å¾éè¦ï¼å ä¸ºå®æç¤ºäºæ¥æ¾è¦ä» document.body è¿ä¸ªä¸ä¸æèç¹å¼å§ãå¦æéæ¼äº "." (å©ä¸ //h2) ï¼æ¥æ¾ä¼ä»æ ¹èç¹ï¼htmlï¼å¤å¼å§ï¼è¿æ ·ä¼å¾æµªè´¹ãï¼
æ¥é å¨ JavaScript ä¸ä½¿ç¨ XPath çç®ä»è·å¾æ´å¤ä¿¡æ¯ã
注æ
- XPath 表达å¼è½å¨ HTML å XML ææ¡£ä¸è®¡ç®ã
- 妿è¦è®¡ç®å«çè䏿¯å½åææ¡£ï¼å¨ FF3 ä¸å¿
须使ç¨
someXMLDoc.evaluate()ï¼è½ç¶å¨ FF2 ä¸document.evaluate()乿æã
ç»æçç±»å
(Merge with Template:XPathResultConstants?
è¿äºæ¯ evaluate æ¹æ³ç resultType åæ°æ¯æçå¼ï¼
| Result Type | Value | Description |
|---|---|---|
ANY_TYPE |
0 | Whatever type naturally results from the given expression. |
NUMBER_TYPE |
1 | A result set containing a single number. Useful, for example, in an XPath expression using the count() function. |
STRING_TYPE |
2 | A result set containing a single string. |
BOOLEAN_TYPE |
3 | A result set containing a single boolean value. Useful, for example, an an XPath expression using the not() function. |
UNORDERED_NODE_ITERATOR_TYPE |
4 | A result set containing all the nodes matching the expression. The nodes in the result set are not necessarily in the same order they appear in the document. |
ORDERED_NODE_ITERATOR_TYPE |
5 | A result set containing all the nodes matching the expression. The nodes in the result set are in the same order they appear in the document. |
UNORDERED_NODE_SNAPSHOT_TYPE |
6 | A result set containing snapshots of all the nodes matching the expression. The nodes in the result set are not necessarily in the same order they appear in the document. |
ORDERED_NODE_SNAPSHOT_TYPE |
7 | A result set containing snapshots of all the nodes matching the expression. The nodes in the result set are in the same order they appear in the document. |
ANY_UNORDERED_NODE_TYPE |
8 | A result set containing any single node that matches the expression. The node is not necessarily the first node in the document that matches the expression. |
FIRST_ORDERED_NODE_TYPE |
9 | A result set containing the first node in the document that matches the expression. |
Results of NODE_ITERATOR types contain references to nodes in the document. Modifying a node will invalidate the iterator. After modifying a node, attempting to iterate through the results will result in an error.
Results of NODE_SNAPSHOT types are snapshots, which are essentially lists of matched nodes. You can make changes to the document by altering snapshot nodes. Modifying the document doesn't invalidate the snapshot; however, if the document is changed, the snapshot may not correspond to the current state of the document, since nodes may have moved, been changed, added, or removed.
è§è
| è§è |
|---|
| DOM > # dom-xpathevaluatorbase-evaluate > |