我想做的是断开选择,并取消一些热键(例如Ctrl+a,Ctrl+c 和 Ctrl+s)
我的代码看起来像这样。
(function($){
$.fn.ctrl = function(key, callback) {
if(typeof key != 'object') key = [key];
callback = callback || function(){
return false;
}
return $(this).keydown(function(e) {
var ret = true;
$.each(key,function(i,k){
if(e.keyCode == k.toUpperCase().charCodeAt(0) && e.ctrlKey) {
ret = callback(e);
}
});
return ret;
});
};
$.fn.disableSelection = function() {
$(window).ctrl(['a','s','c']);
return this.each(function() {
$(this).attr('unselectable', 'on')
.css({
'-moz-user-select':'none',
'-o-user-select':'none',
'-khtml-user-select':'none',
'-webkit-user-select':'none',
'-ms-user-select':'none',
'user-select':'none'
})
.each(function() {
$(this).attr('unselectable','on')
.bind('selectstart',function(){
return false;
});
});
});
}
});
$(document).ready(function() {
$(':not(input,select,textarea)').disableSelection(); <== ERROOR
$("#navigation").treeview({
persist: "location",
collapsed: true,
unique: true
});
});
问题是,当我在 Firefox 上打开页面时,在 firebug 上收到以下错误消息
$(":not(input,select,textarea)").disableSelection is not a function
我错过了什么?有什么建议么?提前谢谢
最佳答案
您忘记将 jQuery 传递给您的函数(以便执行它!):
(function($){
$.fn.ctrl = function(key, callback) { ... }
$.fn.disableSelection = function() { ... }
})(jQuery); // <--- you forgot this!
关于JavaScript "is not a function"问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11526350/