2012-09-04 73 views
0

寻找适用于所有浏览器(包括iPad/iPhone)的解决方案。我已经回顾了过去发布的这个问题,如下工作都很好,除了一个,它是不工作的IE浏览器:使用jQuery选择焦点文本

$("input[type='text'], textarea, input[type='password'], input[type='number']").live('mouseup', function(e) { 
    e.preventDefault(); 
}); 

$("input[type='text'], textarea, input[type='password'], input[type='number']").live('focus', function() { 
    this.setSelectionRange(0, 9999); 
});​ 
+0

正如一个侧面说明,.live现在已被弃用,并且您应该使用jQuery。on –

+0

试图防止'mouseup'的默认操作有什么意义?并且,请描述你想用你的代码完成的事情。只是发布代码并说它不起作用并不能告诉我们你想要做什么。 – jfriend00

+0

这是迟来的后续以下内容:http://stackoverflow.com/questions/9924506/selecting-text-on-focus-using-jquery-not-working-in-iphone-ipad-browsers应该给所有这个问题的背景信息。 – user1045358

回答

0

下面的代码工作很适合我:

$("input[type=text]").live('click', function() { 
    $(this).select(); 
    this.setSelectionRange(0, 9999); 
}); 

但是,尽管最简单的解决方案有缺点,但它不会在使用键盘导航时预先选择文本字段。这个特定问题的替代方案可以在:Looking for a better workaround to Chrome select on focus bug

注意:这个答案是从原始问题的评论4。