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

Javascript 如何选择焦点文本区域中的所有文本(在safari中),javascript,jquery,select,focus,Javascript,Jquery,Select,Focus,我不明白为什么当textarea接收到焦点时,我不能选择textarea的内容 请访问此处的实例: 使用jQuery,这是我的代码。(在SAFARI中)它使文本在单击事件(而不是焦点事件)时被选中: $('textarea.automarkup') .mouseup(function(e){ // fixes safari/chrome problem e.preventDefault(); }) .focus(function(e){ $(this).select();

我不明白为什么当textarea接收到焦点时,我不能选择textarea的内容

请访问此处的实例:

使用jQuery,这是我的代码。(在SAFARI中)它使文本在单击事件(而不是焦点事件)时被选中:

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

这是Safari中唯一有效的东西。专注是行不通的

<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" />

功能SelectAll(id)
{
document.getElementById(id.focus();
document.getElementById(id.select();
}
文本区域:
此文本您可以通过单击此处选择全部 输入文本框:

最简单的方法是将现有代码与计时器组合,因为在WebKit中,
焦点事件通常太早:

JSFIDLE示例:

代码:


我没有Safari浏览器,但我在IE上遇到了同样的问题,并在那里使用了以下工具:

$(document).ready(function() {
    $('body').delegate('textarea.automarkup', 'focus', function(e) {
        var myText = $(this);
        var mytxt = myText.text();
        myText.text(mytxt + "\n event fired: " + e.type);
        myText.select();
    });
    $('body').delegate('textarea.automarkup', 'click', function(e) {
        var myText = $(this);
        var mytxt = myText.text();
        myText.text(mytxt + "\n event fired: " + e.type);
        myText.select();
    });
});
编辑:在玩了一点之后,我使用:

$(document).ready(function(e) {
        $(document).delegate('textarea.automarkup', 'focus click', function(e) {
        var myText = $(this);
        var mytxt = myText.text();
        //myText.text(mytxt + "\n event fired: " + e.type);
        myText.select();
        e.stopImmediatePropagation(); 
        $('#igot').text(mytxt+e.type);
        return false;
    });
});

在本例中:

焦点事件似乎干扰了select方法。短时间延迟后再调用:

<textarea id="ta0" onfocus="
  var inp=this;
  setTimeout(function(){inp.select();},10);
">Here is some text</textarea>
这里有一些文本

考虑到这只是为了应付,也许文本区应该是只读的。

蒂姆·唐恩的回答让我接近了,但在Chrome中单击和拖动时它仍然不起作用,所以我做了这个(Coffeescript)


这听起来像是当有人试图编辑时删除所有内容的方法……很痛苦。在大多数浏览器(和大多数其他应用程序)中,三次单击将选择文本区域的内容。我建议坚持标准的用户界面惯例。内容不应该被编辑,只是被复制。但你有一点。。然而,撇开这些不谈,我想了解一下这里的狩猎和活动的进展情况。。(参见我在JSFIDLE页面上的注释)就我而言,Flash已经死了。在我看来,文本区可以提供原始文本进行复制,当然也没有使用flash那么糟糕。但是,我再次感谢你的意见,但是请让我听听某人关于问题中所述的实际问题的意见。我知道这篇文章发表已经有一段时间了,但是如果有人担心用户在试图复制文本时修改文本,您只需在
标记中添加
只读属性即可;似乎没有必要,也许不是个好主意。返回false就足够了。你为什么要把它放进去?@mikkelbreum:解除绑定的部分是允许
mouseup
事件在文本区域聚焦时正常触发。只有当textarea第一次接收到焦点时,才需要抑制它。诚然,如果焦点是通过键盘而不是键盘,那么之后的第一个
鼠标将被抑制,这并不理想,但在实践中不太可能是一个问题创建名为$this的全局变量。使用'var'关键字限制此变量的范围并防止引入全局变量:
var$this=$(this)@AndyFord:是的,我知道。打字错误谢谢你指出错误。
<textarea id="ta0" onfocus="
  var inp=this;
  setTimeout(function(){inp.select();},10);
">Here is some text</textarea>
$.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
$("textarea").on("focus", function(event) {
  event.preventDefault();
  setTimeout(function() { $(event.target).select(); }, 1); 
});