用 Javascript 获取指定页面元素的位置 - top, left, document, Element, 元素, position, 位置

document.evaluate

marz posted @ Aug 15, 2010 05:28:50 AM in javascript with tags dom Reference xpath , 4032 readers

Returns an XPathResult based on an XPath expression and other given parameters.

Syntax

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 [http://www.w3.org/TR/xpath 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

 

Example

From the Mozilla XPath Tutorial:

   var headings = document.evaluate("//h2", document, null, XPathResult.ANY_TYPE, null);   
   /* Search the document for all h2 elements.   
    * The result will likely be an unordered node iterator. */  
   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); // Alerts the text of all h2 elements  

 

Notes

  • XPath expressions can be evaluated on HTML and XML documents.
  • While using document.evaluate() works in FF2, in FF3 one must use someXMLDoc.evaluate() if evaluating against something other than the current document.

Result types

(Merge with Template:XPathResultConstants?

These are supported values for the resultType parameter of the evaluate method:

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.

Specification

DOM Level 3 XPath

 

Avatar_small
marz said:
Aug 15, 2010 05:42:24 AM

It depends, if your document is an XML DOM document like IE gives you
with XMLHTTP as responseXML then you can do XPath queries with
selectNodes and selectSingleNode e.g.
var tbody = xmlDocument.selectSingleNode("id('main')/tbody");

If your document is a HTML document rendered in IE then there is no
XPath support (respectively no support for XPath over HTML).

That simply example however can be done with the Core and HTML DOM e.g.
var tableBody = document.getElementById('main').tBodies[0];
so you must  sure why you use XPath at all if that is an HTML document.

Harold said:
Nov 25, 2020 08:48:32 PM

Thanks for this share on returning an XPathResult. I was searching for the exact same data and I'm glad that I found this section which contains both coding and description. It helped me to have an idea about how an Xpath is created. plant-based diets

jenniferjohns said:
Jan 12, 2021 02:12:30 PM

Great pleasure to go through your post! best gynaecology hospital in kochi Obviously, it is clear that you have a perfect vision regarding the topic which you have tried to convey to others.

Kohs said:
Jun 13, 2021 02:49:20 AM

Laparoscopic surgery is a surgical technique in laparoscopic surgery for endometriosis in kochi which short, narrow tubes are inserted into the abdomen through small incisions.

juvanta said:
Sep 16, 2021 02:11:26 PM

I have gone through the content we have provided here and it was very well explained and the syntax is really helpful as it will be useful for those who wish to try it out and everthing is well explained in the contents clipping path service company

hkseo said:
Sep 23, 2023 05:04:16 PM

Wow, What an Outstanding post. I found this too much informatics. It is what I was seeking for. I would like to recommend you that please keep sharing such type of info.If possible, Thanks. Delaware startup incorporation

here to know more said:
Dec 15, 2023 09:30:12 PM

Uncertain concerning your own text. It appears to be okay, yet some thing can be missing

Visit this link said:
Aug 20, 2024 01:42:06 PM

It might solidify best to ro with your dealing with postpartum depression without the need for paxil if you are strapped regarding it.

jack said:
Sep 06, 2024 01:37:30 AM

The main three also provided plenty of laughs and played off each other perfectly well, with Charlie Day being the best of the three. <a href="https://caluaniemuelearchemicals.com/product/caluanie-muelear-oxidize-5l/">https://caluaniemuelearchemicals.com/product/caluanie-muelear-oxidize-5l/</a>

best casino bonuses said:
Sep 14, 2024 09:53:16 PM

An excellent presentation. Clear. Practical. Insightful. Shows a depth of experience. Thank you. I learned a great deal..


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter