Get offset of node in whole html document in Javascript?

I am looking for a way to get the offset relative to the entire document for a given node using Javascript. For instance:.

<html>
    <head></head>
    <body>
        <div id="mainContent">
            <h1 id="myTitle">Title</h1>
            <p>First paragraph</p>
            <p>Second <b>paragraph</b></p>
        </div>
    </body>
</html>

Then (using jQuery):

$("#myTitle").getDocumentOffset()

should return 47 because h1 with id myTitle begins at offset 47 in characters relative to the entire document.

Any idea how to do this?

Thanks in advance.

+3
source share
1 answer

This question does not make sense, because extra spaces are ignored in HTML documents, and therefore the same page can be represented in several ways. For example, this:

<html>
  <body></body>
</html>

equivalent to this:

<html><body></body></html>

, , , HTML- ( ), . , "", , .

+7

Source: https://habr.com/ru/post/1769886/


All Articles