简体   繁体   English

jQuery 代理函数和事件

[英]jQuery proxy functions and events

Look at the following jquery plugin I have made:看看我制作的以下jquery插件:

(function($) {

    //
    // Constructor.
    //

    var Tb = function(element, options) {
        var self = this;

        this.options = options;
        this.$element = $(element);
        this.$input = $(element).find('.tb-input');
        this.$label = $(element).find('.tb-label');

        this.$element
            .proxy('click', this.$input.click)
            .proxy('val', this.$input.val);
    };

    Tb.prototype = {
        constructor: Tb
    };

  //
    // jQuery plugin.
    //

    $.fn.tb = function(option) {
        return this.each(function() {
            var $this = $(this)
                , data = $this.data('tb')
                , options = $.extend({}, $.fn.tb.defaults, $this.data(), typeof option == 'object' && option);

            if (!data) {
                $this.data('tb', (data = new Tb(this, options)));
            }

            if (typeof(option) == 'string') {
                data[option]();
            }
        });
    };

    $.fn.tb.defaults = {};
    $.fn.tb.Constructor = Tb;

})(window.jQuery);

HTML (demo) HTML(演示)

<div id="tb-user-name" class="tb">
    <label class="tb-label">This is the placeholder</label>
    <input type="text" class="tb-input" />
</div>

javascript init: javascript初始化:

$('#tb-user-name').tb();

So basically If I do this:所以基本上如果我这样做:

$('#tb-user-name').val(); // Should return the value of the input not the "container".
$('#tb-user-name').focus(); // Should focus on the input, not the "container"

But my code is not working, how can I do this?但是我的代码不起作用,我该怎么做? I have tried "on" but that doesnt work either, does work on focus with a little work around but "val" is not an event but a function.我试过“on”,但这也不起作用,确实可以通过一些工作来集中注意力,但“val”不是事件而是函数。

UPDATE (Working but hacky)更新(工作但hacky)

http://jsfiddle.net/tppiotrowski/WSdmL/3/ http://jsfiddle.net/tppiotrowski/WSdmL/3/

Thanks for @teddybeard for this solution, this is however not the best way because this is kinda hacky, and since on EVERY key event the val event gets triggered, I am looking for an alternative, if anyone can help me with that that would be awesome.感谢@teddybeard 提供此解决方案,但这不是最好的方法,因为这有点 hacky,并且由于在每个关键事件上都会触发 val 事件,我正在寻找替代方案,如果有人可以帮助我,那将是惊人的。

Check out this fiddle: http://jsfiddle.net/tppiotrowski/WSdmL/3/看看这个小提琴: http : //jsfiddle.net/tppiotrowski/WSdmL/3/

What you are trying to do is override the default focus() and val() behavior of jQuery when used on a specific element.您尝试做的是在特定元素上使用 jQuery 时覆盖默认的focus()val()行为。 Below, I've overridden the val() and focus() functions to have special behavior when called by an element with class .tb :下面,我重写了val()focus()函数,使其在被具有类.tb的元素调用时具有特殊行为:

(function($) {
    var originalVal = $.fn.val;
    $.fn.val = function(value) {
        var self = this;
        if (this.hasClass('tb')) self = this.find('.tb-input').first();
        if (typeof value == 'undefined') {
            return originalVal.call(self);
        } else {
            return originalVal.call(self, value);
        }
    };
    var originalFocus = $.fn.focus;
    $.fn.focus = function(value) {
        var self = this;
        if (this.hasClass('tb')) self = this.find('.tb-input').first();
        if (typeof value == 'undefined') {
            return originalFocus.call(self);
        } else {
            return originalFocus.call(self, value);
        }
    };
})(jQuery);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM