2013-10-16 38 views
1
$('input').on('focus', function() { 
    $(this).val('example'); 
    $(this).attr('unselectable', 'on').css('user-select', 'none').on('startselect', false); 
}); 

当我尝试在Chrome或Opera上选择字段值时,我能够做到这一点。 在Firefox和IE浏览器上没问题。无法选择无法在webkit上工作

有什么办法可以禁用输入字段中的文本选择?

IE 10,铬30,歌剧17.

来源:http://jsfiddle.net/VmDDY/

+0

我相信事件是'selectstart',而不是'startselect'。不知道这是否能解决您的问题。 –

+0

仍然一样。 – user2886388

+0

我认为这[链接](https://developer.mozilla.org/en-US/docs/Web/CSS/user-select?redirectlocale=en-US&redirectslug=CSS/user-select)表明该功能不是由您提到的浏览器支持 –

回答

1

尝试此,

CSS

.unselectable { 
    -moz-user-select: none; 
    -webkit-user-select: none; 
    -ms-user-select: none; 
    user-select: none; 
} 

SCRIPT

$('input').on('focus', function() { 
    $(this).val('example'); 
    $(this).attr('unselectable', 'on') 
      .addClass('unselectable').on('startselect', false); 
    //add class ------^ 
}); 

歌剧不支持user-select

0

您需要使用所有正确的CSS变化:

-webkit-touch-callout: none; 
-webkit-user-select: none; 
-khtml-user-select: none; 
-moz-user-select: none; 
-ms-user-select: none; 
user-select: none; 

但这link告诉该功能不是由你所提到的浏览器支持