【问题标题】:How do I call a sub-function from within a function object in javascript如何从javascript中的函数对象中调用子函数
【发布时间】:2011-03-30 11:34:28
【问题描述】:

我已经检查了有关堆栈溢出的相关问题,但似乎无法找到解决我困境的答案。我正在尝试使用 javascript 插件(标记它!-标记编辑器),我需要找到一种方法来调用它的一个函数“create_choice()”编辑:在之后的某个时间已启动。调用后有没有办法:

$tagit = $("#mytags").tagit();

然后我可以调用类似的东西

$tagit.create_choice('test123');

这是示例的链接: http://levycarneiro.com/projects/tag-it/example.html

如果有帮助,下面是插件的代码

(function($) {

    $.fn.tagit = function(options) {

        var el = this;

        const BACKSPACE        = 8;
        const ENTER            = 13;
        const SPACE            = 32;
        const COMMA            = 44;

        // add the tagit CSS class.
        el.addClass("tagit");

        // create the input field.
        var html_input_field = "<li class=\"tagit-new\"><input class=\"tagit-input\" type=\"text\" /></li>\n";
        el.html (html_input_field);

        tag_input        = el.children(".tagit-new").children(".tagit-input");

        $(this).click(function(e){
            if (e.target.tagName == 'A') {
                // Removes a tag when the little 'x' is clicked.
                // Event is binded to the UL, otherwise a new tag (LI > A) wouldn't have this event attached to it.
                $(e.target).parent().remove();
            }
            else {
                // Sets the focus() to the input field, if the user clicks anywhere inside the UL.
                // This is needed because the input field needs to be of a small size.
                tag_input.focus();
            }
        });

        tag_input.keypress(function(event){
            if (event.which == BACKSPACE) {
                if (tag_input.val() == "") {
                    // When backspace is pressed, the last tag is deleted.
                    $(el).children(".tagit-choice:last").remove();
                }
            }
            // Comma/Space/Enter are all valid delimiters for new tags.
            else if (event.which == COMMA || event.which == SPACE || event.which == ENTER) {
                event.preventDefault();

                var typed = tag_input.val();
                typed = typed.replace(/,+$/,"");
                typed = typed.trim();

                if (typed != "") {
                    if (is_new (typed)) {
                        create_choice (typed);
                    }
                    // Cleaning the input.
                    tag_input.val("");
                }
            }
        });

        tag_input.autocomplete({
            source: options.availableTags, 
            select: function(event,ui){
                if (is_new (ui.item.value)) {
                    create_choice (ui.item.value);
                }
                // Cleaning the input.
                tag_input.val("");

                // Preventing the tag input to be update with the chosen value.
                return false;
            }
        });

        function is_new (value){
            var is_new = true;
            this.tag_input.parents("ul").children(".tagit-choice").each(function(i){
                n = $(this).children("input").val();
                if (value == n) {
                    is_new = false;
                }
            })
            return is_new;
        }
        function create_choice (value){
            var el = "";
            el  = "<li class=\"tagit-choice\">\n";
            el += value + "\n";
            el += "<a class=\"close\">x</a>\n";
            el += "<input type=\"hidden\" style=\"display:none;\" value=\""+value+"\" name=\"item[tags][]\">\n";
            el += "</li>\n";
            var li_search_tags = this.tag_input.parent();
            $(el).insertBefore (li_search_tags);
            this.tag_input.val("");
        }
    };

    String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g,"");
    };

})(jQuery);

【问题讨论】:

  • 可能类似于var tagit = $("#mytags").tagit(get|set:'test123');
  • (a) tagit 似乎没有返回任何东西。 (b) create_choice 只是函数的本地,你不能从外部访问它。你想达到什么目的?否则答案是:不可能。

标签: javascript jquery function call


【解决方案1】:

我在http://jsfiddle.net/nickywaites/DnkBt/ 创建了一个工作示例,但它确实需要对插件进行更改。

【讨论】:

  • 感谢您花时间更改代码 nicky!很棒的工作!
【解决方案2】:

改变

$.fn.tagit = function(options) { ...

$.fn.tagit = function(options,callback) { ...

添加

if (callback && typeof callback == 'function') {
    callback();
}

之后

String.prototype.trim = function() {
   return this.replace(/^\s+|\s+$/g,"");
};

现在您可以在 tagit 调用之后立即调用您选择的函数:

$tagit = $("#mytags").tagit(yourOptions, function(){
    alert('hi')!
});

【讨论】:

  • 抱歉,ezmilhouse,我已经重新编辑了我的问题,以便更清楚一点,我希望能够在将来的某个时候调用该函数,而不仅仅是在插件实例化之后。
【解决方案3】:

你可以尝试添加

return this;

就在函数 create_choice 块之后。 tagit 将返回自身,您可以调用 make_choice 或 .fn.tagit 中包含的任何函数

【讨论】:

  • 我试过了,但它说 $tagit.create_choice 不是函数
  • Felix Kling 是对的,插件需要稍微重写。 tagit 函数应该返回一个对象,其方法包括 create_choice 函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多