jQuery获取附加的类名

时间:2014-08-14 19:29:41

标签: jquery jquery-plugins

我正在创建一个jQuery插件,我在添加点击监听器时遇到问题

(function ($) {
    $.fn.rockon = function () {
        return this.each(function () {
            var $this = $(this);

            $(document).on('click', '.rockon', function () {

            });
       });
    };
})(jQuery);

我将插件初始化为

$(".rockon").rockon();

但是,不要听" .rockon"点击一下,我怎么能听到我附加插件的元素。

我通常会这样做

$this.attr("class");

但该元素有多个类

2 个答案:

答案 0 :(得分:2)

你是说这个意思吗?的 DEMO

(function ($) {
    $.fn.rockon = function () {
        return this.each(function () {
            var $this = $(this);

            $(document).on('click', this, function () {

            });
       });
    };
})(jQuery);

这将侦听所选元素上的click事件!

答案 1 :(得分:0)

为什么不

        $.fn.rockon = function () {
            return this.each(function () {
                var $this = $(this);
                $this.on('click', function () {
                    console.log("click");
                });
            });
        };