简体   繁体   English

jQuery - 从textarea中选择所有文本

[英]jQuery - select all text from a textarea

How can I make it so when you click inside a textarea, its entire content gets selected? 当你在textarea里面点击时,我怎么能这样做,它的整个内容都被选中了?

And eventually when you click again, to deselect it. 最后再次点击时,取消选择它。

To stop the user from getting annoyed when the whole text gets selected every time they try to move the caret using their mouse, you should do this using the focus event, not the click event. 为了防止用户在每次尝试使用鼠标移动插入符号时选择整个文本时会感到恼火,您应该使用focus事件而不是click事件来执行此操作。 The following will do the job and works around a problem in Chrome that prevents the simplest version (ie just calling the textarea's select() method in a focus event handler) from working. 以下将完成这项工作并解决Chrome中的一个问题,该问题阻止了最简单的版本(即只调用focus事件处理程序中的textarea的select()方法)。

jsFiddle: http://jsfiddle.net/NM62A/ jsFiddle: http//jsfiddle.net/NM62A/

Code: 码:

<textarea id="foo">Some text</textarea>

<script type="text/javascript">
    var textBox = document.getElementById("foo");
    textBox.onfocus = function() {
        textBox.select();

        // Work around Chrome's little problem
        textBox.onmouseup = function() {
            // Prevent further mouseup intervention
            textBox.onmouseup = null;
            return false;
        };
    };
</script>

jQuery version: jQuery版本:

$("#foo").focus(function() {
    var $this = $(this);
    $this.select();

    // Work around Chrome's little problem
    $this.mouseup(function() {
        // Prevent further mouseup intervention
        $this.unbind("mouseup");
        return false;
    });
});

Better way, with solution to tab and chrome problem and new jquery way 更好的方法,解决了tab和chrome问题以及新的jquery方式

$("#element").on("focus keyup", function(e){

        var keycode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
        if(keycode === 9 || !keycode){
            // Hacemos select
            var $this = $(this);
            $this.select();

            // Para Chrome's que da problema
            $this.on("mouseup", function() {
                // Unbindeamos el mouseup
                $this.off("mouseup");
                return false;
            });
        }
    });

I ended up using this: 我最终使用了这个:

$('.selectAll').toggle(function() {
  $(this).select();
}, function() {
  $(this).unselect();
});

Slightly shorter jQuery version: 略短的jQuery版本:

$('your-element').focus(function(e) {
  e.target.select();
  jQuery(e.target).one('mouseup', function(e) {
    e.preventDefault();
  });
});

It handles the Chrome corner case correctly. 它正确处理Chrome角落的情况。 See http://jsfiddle.net/Ztyx/XMkwm/ for an example. 有关示例,请参见http://jsfiddle.net/Ztyx/XMkwm/

$('textarea').focus(function() {
    this.select();
}).mouseup(function() {
    return false;
});

Selecting text in an element (akin to highlighting with your mouse) 选择元素中的文本(类似于用鼠标突出显示)

:) :)

Using the accepted answer on that post, you can call the function like this: 使用该帖子上接受的答案,您可以像这样调用函数:

$(function() {
  $('#textareaId').click(function() {
    SelectText('#textareaId');
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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