首页 新闻 赞助 找找看

jQuery获取文本框中选中的内容

0
[已解决问题] 解决于 2015-01-20 14:55

如何使用jQuery获取文本框中鼠标选中的内容,并将选中的内容情况。

(liu)的主页 (liu) | 初学一级 | 园豆:156
提问于:2015-01-12 16:30
< >
分享
最佳答案
0

$(".className").val();

奖励园豆:5
小小鸟努力飞翔 | 初学一级 |园豆:34 | 2015-01-12 18:19

文本框中 获得使用鼠标选中的内容,不是文本框中的全部的内容。

(liu) | 园豆:156 (初学一级) | 2015-01-15 17:58

@(liu): 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title> 测试选中文字 </title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
        body {
            font-size: 12px;
        }

        #show {
            background-color: #CCFF99;
        }
    </style>
</head>
<body>
    <textarea id="content" cols="30" rows="10">
        测试文字1测试文字2
    </textarea>
    <button id="btn">获取选中的文字</button>
    <div id="show"></div>
    <script>
        function getSelectText2(id) {
            var t = document.getElementById(id);
            if (window.getSelection) {
                if (t.selectionStart != undefined && t.selectionEnd != undefined) {
                    return t.value.substring(t.selectionStart, t.selectionEnd);
                } else {
                    return "";
                }
            } else {
                return document.selection.createRange().text;
            }
        }
        document.getElementById('btn').onclick = function () {
            document.getElementById('show').innerHTML = getSelectText2('content');
        }
    </script>
</body>
</html>   
秋壶冰月 | 园豆:5903 (大侠五级) | 2015-01-15 18:08

@秋壶冰月: 谢了,使用这个方法,解决了。

(liu) | 园豆:156 (初学一级) | 2015-01-20 14:55
其他回答(1)
0
秋壶冰月 | 园豆:5903 (大侠五级) | 2015-01-12 16:37

谢谢,有简单点的方法吗

支持(0) 反对(0) (liu) | 园豆:156 (初学一级) | 2015-01-15 17:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册