简体   繁体   中英

JavaScript/jQuery get current selected text inside iframe

My Iframe looks like this

<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>

This iframe behaves like textarea (it is like input field) and I can write text into it and select some text inside. Now When button is pressed I want to get selected area (start position and end position of body tag selection). How to? JQuery? Pure JS? Examples?

If you just want the selected text as the question title suggests, my answer here will do the job (but only for iframes that come from the same domain as the containing document):

how to get selected text from iframe with javascript?

If you're looking for character offsets within the <body> , as the question body suggests, here's how you can do that:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM