her*_*ron 2 javascript jquery function
我要做的是断开选择,取消一些热键(如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
});
});
Run Code Online (Sandbox Code Playgroud)
问题是,当我在Firefox上打开页面时,在firebug上收到以下错误消息
$(":not(input,select,textarea)").disableSelection is not a function
Run Code Online (Sandbox Code Playgroud)
我错过了什么?有什么建议?Thx提前
Nea*_*eal 11
你忘了将jQuery传递给你的函数(为了执行它!):
(function($){
$.fn.ctrl = function(key, callback) { ... }
$.fn.disableSelection = function() { ... }
})(jQuery); // <--- you forgot this!
Run Code Online (Sandbox Code Playgroud)