JavaScript "is not a function"问题

标签 javascript jquery function

我想做的是断开选择,并取消一些热键(例如Ctrl+aCtrl+cCtrl+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/

相关文章:

javascript - 跨列跳跃的 CSS column-count 元素

c++ - 我应该专攻还是重载?

Javascript 函数数组

javascript - 如何保持对话框显示并提醒 NativeScript

Javascript If 语句,遍历数组

javascript - 从 JSON 数组中提取 JSON 对象

javascript - 使用 jQuery 和内存泄漏

Javascript html函数输入按钮输出

c++ - 函数执行时间很奇怪

javascript - 异步 js 验证表单