Get the highlighted text

落爺英雄遲暮 提交于 2019-12-06 13:34:03

问题


When I select some text in the <div>, I want that highlighted text to appear in the textbox which is just below the div. How can I do it?

<div>
    My text goes here.
</div>
<asp:TextBox ID="txt" runat="server"/>

回答1:


working demo http://jsfiddle.net/KgtW5/ or using DIV demo http://jsfiddle.net/KgtW5/3/

.on API: http://api.jquery.com/on/

I have customized it for your need man.

Good link: and BIG hint: Get the Highlighted/Selected text

Hope demo helps you, lemme know if I missed anything! :)

code

$('textarea').on('select', function() {
    var foo = getSelectionText();
    $('#hulk').val(foo);
});


function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}​

html

<textarea>Some default text; HUlk is very cool innit</textarea>
<br/>

<input type="text" id="hulk" />
​

Image



来源:https://stackoverflow.com/questions/11272586/get-the-highlighted-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!