jQuery - select all text from a textarea

后端 未结 6 2042
情书的邮戳
情书的邮戳 2020-11-27 12:03

How can I make it so when you click inside a textarea, its entire content gets selected?

And eventually when you click again, to deselect it.

6条回答
  •  一向
    一向 (楼主)
    2020-11-27 12:43

    Better way, with solution to tab and chrome problem and new jquery way

    $("#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;
                });
            }
        });
    

提交回复
热议问题