2013-10-16 39 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告訴該功能不是由你所提到的瀏覽器支持