【问题标题】:JavaScript/jQuery get current selected text inside iframeJavaScript/jQuery 在 iframe 中获取当前选定的文本
【发布时间】:2011-11-07 18:24:05
【问题描述】:

我的 iframe 看起来像这样

<iframe id="myId" class="af_richTextEditor_content-input" frameborder="0" style="height:     36em;" src="/blank.html">
    <html>
        <head>
            <title></title>
        </head>
        <body dir="ltr" style="margin: 0px; padding: 1px; font-size: small; font-family: Tahoma,Verdana,Arial,Helvetica,FreeSans,sans-serif; -moz-box-sizing: border-box;">
            myText
            <br>
            MySecondText
        </body>
    </html>
</iframe>

这个 iframe 的行为类似于 textarea(它就像输入字段),我可以在其中写入文本并在其中选择一些文本。现在按下按钮时,我想获得选定区域(主体标签选择的开始位置和结束位置)。如何?查询?纯JS?例子?

【问题讨论】:

标签: javascript jquery html iframe cursor


【解决方案1】:

如果您只想要问题标题所暗示的所选文本,我的回答将完成这项工作(但仅适用于与包含文档来自同一域的 iframe):

how to get selected text from iframe with javascript?

如果您正在寻找 &lt;body&gt; 中的字符偏移量,正如问题正文所建议的那样,您可以这样做:

Get a range's start and end offset's relative to its parent container

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 2016-02-02
    • 2023-04-07
    • 1970-01-01
    • 2017-09-12
    • 2013-07-12
    • 2010-12-01
    相关资源
    最近更新 更多