<legend id='KFhEq'><style id='KFhEq'><dir id='KFhEq'><q id='KFhEq'></q></dir></style></legend>
      <tfoot id='KFhEq'></tfoot>

    1. <small id='KFhEq'></small><noframes id='KFhEq'>

    2. <i id='KFhEq'><tr id='KFhEq'><dt id='KFhEq'><q id='KFhEq'><span id='KFhEq'><b id='KFhEq'><form id='KFhEq'><ins id='KFhEq'></ins><ul id='KFhEq'></ul><sub id='KFhEq'></sub></form><legend id='KFhEq'></legend><bdo id='KFhEq'><pre id='KFhEq'><center id='KFhEq'></center></pre></bdo></b><th id='KFhEq'></th></span></q></dt></tr></i><div id='KFhEq'><tfoot id='KFhEq'></tfoot><dl id='KFhEq'><fieldset id='KFhEq'></fieldset></dl></div>

      • <bdo id='KFhEq'></bdo><ul id='KFhEq'></ul>
    3. 计算所选文本 javascript/JQuery 的位置?

      时间:2023-07-31

      <i id='h68ey'><tr id='h68ey'><dt id='h68ey'><q id='h68ey'><span id='h68ey'><b id='h68ey'><form id='h68ey'><ins id='h68ey'></ins><ul id='h68ey'></ul><sub id='h68ey'></sub></form><legend id='h68ey'></legend><bdo id='h68ey'><pre id='h68ey'><center id='h68ey'></center></pre></bdo></b><th id='h68ey'></th></span></q></dt></tr></i><div id='h68ey'><tfoot id='h68ey'></tfoot><dl id='h68ey'><fieldset id='h68ey'></fieldset></dl></div>

        <tbody id='h68ey'></tbody>
        • <bdo id='h68ey'></bdo><ul id='h68ey'></ul>

          <tfoot id='h68ey'></tfoot>

            <legend id='h68ey'><style id='h68ey'><dir id='h68ey'><q id='h68ey'></q></dir></style></legend>
          1. <small id='h68ey'></small><noframes id='h68ey'>

                本文介绍了计算所选文本 javascript/JQuery 的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                如何通过计算正文标记后的偏移量来检索选定文本的位置?

                How to retrieve the position of selected text by calculating it's offset immediate after body tag?

                例如考虑以下html,

                For Example consider the following html,

                <body> <div>hi</div> <div>dude!</div> </body>
                

                在从i"(嗨)到du"(在花花公子)中选择时,我需要将 2 作为起始位置 &4 作为结束位置.

                on selecting from "i" (in hi) to "du" (in dude), I need to get 2 as start position & 4 as end position.

                注意:忽略标签元素.

                最好使用Javascript!

                Javascript is preferable!

                推荐答案

                这里有一些简单、幼稚的代码来执行此操作,它们可能很适合您的用例.它不考虑任何可能变得不可见的文本(例如,通过 CSS 或通过在 a or 元素内),并且可能存在带有换行符的浏览器差异(IE 与其他所有内容),并且不考虑折叠的空白(例如 2 个或更多连续的空格字符折叠到页面上的一个可见空间).但是,它在所有主流浏览器中都适用于您的示例.

                Here's some simple, naive code to do this that may well do the job for your use case. It does not take into account any text that may be made invisible (either by CSS or by being inside a or element, for example) and may have browser discrepancies (IE versus everything else) with line breaks, and takes no account of collapsed whitespace (such as 2 or more consecutive space characters collapsing to one visible space on the page). However, it does work for your example in all major browsers.

                现场演示:http://jsfiddle.net/UuDpL/2/

                代码:

                function getSelectionCharOffsetsWithin(element) {
                    var start = 0, end = 0;
                    var sel, range, priorRange;
                    if (typeof window.getSelection != "undefined") {
                        range = window.getSelection().getRangeAt(0);
                        priorRange = range.cloneRange();
                        priorRange.selectNodeContents(element);
                        priorRange.setEnd(range.startContainer, range.startOffset);
                        start = priorRange.toString().length;
                        end = start + range.toString().length;
                    } else if (typeof document.selection != "undefined" &&
                            (sel = document.selection).type != "Control") {
                        range = sel.createRange();
                        priorRange = document.body.createTextRange();
                        priorRange.moveToElementText(element);
                        priorRange.setEndPoint("EndToStart", range);
                        start = priorRange.text.length;
                        end = start + range.text.length;
                    }
                    return {
                        start: start,
                        end: end
                    };
                }
                
                alert( getSelectionCharOffsetsWithin(document.body).start );
                

                这篇关于计算所选文本 javascript/JQuery 的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:XML 文件中的自闭合标签 下一篇:在 JavaScript 的 HTML 标记中存储任意信息?

                相关文章

                <small id='eCzOa'></small><noframes id='eCzOa'>

                <i id='eCzOa'><tr id='eCzOa'><dt id='eCzOa'><q id='eCzOa'><span id='eCzOa'><b id='eCzOa'><form id='eCzOa'><ins id='eCzOa'></ins><ul id='eCzOa'></ul><sub id='eCzOa'></sub></form><legend id='eCzOa'></legend><bdo id='eCzOa'><pre id='eCzOa'><center id='eCzOa'></center></pre></bdo></b><th id='eCzOa'></th></span></q></dt></tr></i><div id='eCzOa'><tfoot id='eCzOa'></tfoot><dl id='eCzOa'><fieldset id='eCzOa'></fieldset></dl></div>
                  <bdo id='eCzOa'></bdo><ul id='eCzOa'></ul>

                  1. <legend id='eCzOa'><style id='eCzOa'><dir id='eCzOa'><q id='eCzOa'></q></dir></style></legend><tfoot id='eCzOa'></tfoot>