首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在焦点上选择文本区域中的所有文本(在safari中)

如何在焦点上选择文本区域中的所有文本(在safari中)
EN

Stack Overflow用户
提问于 2011-06-01 20:39:04
回答 6查看 20.2K关注 0票数 15

我不明白为什么当文本区域接收到焦点时,我不能让文本区域的内容被选中。

请访问此处的现场示例:http://jsfiddle.net/mikkelbreum/aSvst/1

使用jQuery,这是我的代码。(在SAFARI中)它在单击事件的情况下使文本处于选中状态,而不是焦点事件:

代码语言:javascript
复制
$('textarea.automarkup')
.mouseup(function(e){
    // fixes safari/chrome problem
    e.preventDefault();
})
.focus(function(e){
    $(this).select();
})
.click(function(e){
    $(this).select();
});
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-06-01 21:17:46

最简单的做法是将现有代码与计时器相结合,因为在WebKit中focus事件通常太早了:

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

代码:

代码语言:javascript
复制
$('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);
});
票数 35
EN

Stack Overflow用户

发布于 2011-06-01 21:11:16

这是Safari中唯一可以工作的东西。专注是行不通的。

代码语言:javascript
复制
<script type="text/javascript">
function SelectAll(id)
{
    document.getElementById(id).focus();
    document.getElementById(id).select();
}
</script>

Textarea:<br>
<textarea rows="3" id="txtarea" onclick="SelectAll('txtarea');" onfocus="SelectAll('txtarea');" style="width:200px" >This text you can select all by clicking here </textarea>

Input TextBox:<br>
<input type="text" id="txtfld" onclick="SelectAll('txtfld');" style="width:200px" value = "This text you can select all" />
票数 3
EN

Stack Overflow用户

发布于 2012-12-04 05:44:47

Tim Down的回答让我很接近,但在Chrome中点击和拖动时仍然不起作用,所以我这样做了(Coffeescript)。

代码语言:javascript
复制
$.fn.extend
    copyableInput: ->
        @.each ->
            $input = $(this)

            # Prevent the input from being edited (but allow it to be selected)
            $input.attr 'readonly', "readonly"

            if $input.is 'textarea'
                $input.unbind('focus').focus ->
                    input = $(this).select()
                    $input.data 'selected', true

                    setTimeout ->
                        input.select()
                    , 0

                # Work around WebKit's little problem
                $input.bind 'mousedown mouseup', (e) ->
                    $input.select()

                    if $input.data('selected') == true
                        e.preventDefault()
                        return false


                $input.unbind('blur').blur ->
                    $input.data 'selected', false

            else
                # Select all the text when focused (I'm intentionally using click for inputs: http://stackoverflow.com/questions/3150275/jquery-input-select-all-on-focus)
                $input.unbind('click').click ->
                    input = $(this).select();

                    setTimeout ->
                        input.select()
                    , 0
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6201278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档