寻找更好的解决方法,Chrome选择焦点错误 [英] Looking for a better workaround to Chrome select on focus bug

查看:168
本文介绍了寻找更好的解决方法,Chrome选择焦点错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到与这个问题,这是由于 Webkit中的这个错误。但是,提供的解决方法不适用于我的应用程序。让我重新陈述这个问题,这样你就不必去读另一个问题了:

I have the same problem as the user in this question, which is due to this bug in Webkit. However, the workaround provided will not work for my app. Let me re-state the problem so that you don't have to go read another question:

我试图选择textarea中的所有文本。以下jQuery代码适用于IE / FF / Opera:

I am trying to select all the text in a textarea when it gets focus. The following jQuery code works in IE/FF/Opera:

$('#out').focus(function(){
  $('#out').select();
});

然而,在Chrome / Safari中,文本被选中 - 非常简短 - 但随后是mouseUp事件被触发并取消选择文本。以上链接提供了以下解决方法:

However, in Chrome/Safari the text is selected--very briefly--but then the mouseUp event is fired and the text is deselected. The following workaround is offered in the above links:

$('#out').mouseup(function(e){
  e.preventDefault();
});

然而,此解决方法对我没有好处。我想在用户给出textarea焦点时选择所有文本 only 。如果他选择的话,他必须然后才能选择部分文本。有人能想到仍然符合这个要求的解决方法吗?

However, this workaround is no good for me. I want to select all text only when the user gives the textarea focus. He must then be able to select only part of the text if he chooses. Can anyone think of a workaround that still meets this requirement?

推荐答案

这个怎么样?

$('#out').focus(function () {
    $('#out').select().mouseup(function (e) {
        e.preventDefault();
        $(this).unbind("mouseup");
    });
});

这篇关于寻找更好的解决方法,Chrome选择焦点错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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