如何选择焦点上的textarea中的所有文本(在safari中) [英] how to select all text in textarea on focus (in safari)

查看:161
本文介绍了如何选择焦点上的textarea中的所有文本(在safari中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



请访问一个实际的例子,我们可以看到一个textarea文件的内容。这里: http://jsfiddle.net/mikkelbreum/aSvst/1



使用jQuery,这是我的代码。 (IN SAFARI)它使得在点击事件的情况下选择文本,而不是焦点事件:

  $('textarea .automarkup')
.mouseup(function(e){
//修正safari / chrome问题
e.preventDefault();
})
.focus(函数(e){
$(this).select();
})
.click(function(e){
$(this).select();
});


解决方案

最简单的做法是将现有的代码有一个计时器,因为焦点事件在WebKit中一般来说太早了:

jsFiddle例子:http://jsfiddle.net/NWaav/2/



代码: p>

  $('textarea.automarkup')。焦点(函数(){
var $ this = $(this); ();

$ this.select();

window.setTimeout(function(){
$ this.select();
},1) ;

//解决WebKit的小问题
函数mouseUpHandler(){
//防止进一步的mouseup干预
$ this.off(mouseup,mouseUpHandler) ;
return false;
}

$ this.mouseup(mouseUpHandler);
});


I can't work out why I can't get the the content of a textarea to be selected when the textarea receives focus.

Please visit a live example here: http://jsfiddle.net/mikkelbreum/aSvst/1

Using jQuery, this is my code. (IN SAFARI) It makes the text selected in case of the click event, but not the focus event:

$('textarea.automarkup')
.mouseup(function(e){
    // fixes safari/chrome problem
    e.preventDefault();
})
.focus(function(e){
    $(this).select();
})
.click(function(e){
    $(this).select();
});

解决方案

The simplest thing to do is to combine your existing code with a timer, since the focus event is generally too early in WebKit:

jsFiddle example: http://jsfiddle.net/NWaav/2/

Code:

$('textarea.automarkup').focus(function() {
    var $this = $(this);

    $this.select();

    window.setTimeout(function() {
        $this.select();
    }, 1);

    // Work around WebKit's little problem
    function mouseUpHandler() {
        // Prevent further mouseup intervention
        $this.off("mouseup", mouseUpHandler);
        return false;
    }

    $this.mouseup(mouseUpHandler);
});

这篇关于如何选择焦点上的textarea中的所有文本(在safari中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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