获取高亮文本 [英] Get the highlighted text

查看:136
本文介绍了获取高亮文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我选择在&LT一些文本; DIV> ,我想要突出显示的文字出现在这仅仅是在div下方的文本框中。我该怎么办呢?

 < D​​IV>
    我的文字放在这里。
< / DIV>
< ASP:文本框ID =TXT=服务器/>


解决方案

工作演示 HTTP: //jsfiddle.net/KgtW5/ 或使用DIV演示 的http:/ /jsfiddle.net/KgtW5/3/

。对 API:的http:// API。 jquery.com/on/

我已经定制为您的需要的人。

很好的链接:和BIG提示:的jQuery:获取高亮文本

希望演示可以帮助你,还是让我知道,如果我错过了什么! :)

code

  $('textarea的')。在('选择',函数(){
    无功富= getSelectionText();
    $('#绿巨人)VAL(富)。
});
功能getSelectionText(){
    VAR文本=;
    如果(window.getSelection){
        。文字= window.getSelection()的toString();
    }否则如果(document.selection&安培;&安培;!document.selection.type =控制){
        。文字= document.selection.createRange()文本;
    }
    返回文本;
}

HTML

 < TextArea>有的默认文本;绿巨人是非常酷的是吗< / textarea的>
< BR /><输入类型=文本ID =绿巨人/>

图片

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

解决方案

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: jQuery: get the highlighted 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

这篇关于获取高亮文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆