使用jQuery在Safari和Chrome中无法选择焦点上的文本

use*_*550 84 safari jquery select google-chrome focus

我有以下jQuery代码(类似于这个问题),可以在Firefox和IE中使用,但在Chrome和Safari中失败(没有错误,只是不起作用).任何解决方法的想法?

$("#souper_fancy").focus(function() { $(this).select() });
Run Code Online (Sandbox Code Playgroud)

小智 187

这是导致选择未被选中的onmouseup事件,因此您只需要添加:

$("#souper_fancy").mouseup(function(e){
    e.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)

  • 有关此错误的更多内容:http://code.google.com/p/chromium/issues/detail?id = 4505 (3认同)
  • 我想在iPad/iPhone的野生动物园中确切行为.这在iPod/iPhone浏览器中不起作用.任何线索. (2认同)

Ale*_*007 25

$('#randomfield').focus(function(event) {
    setTimeout(function() {$('#randomfield').select();}, 0);
});
Run Code Online (Sandbox Code Playgroud)

  • 如果您正尝试在Android上运行的PhoneGap应用的表单字段中选择文本,这是最佳答案.这为用户提供了选择文本的可视指示,而接受的答案则没有. (3认同)