我正在创建一个jQuery插件,我在添加点击监听器时遇到问题
(function ($) {
$.fn.rockon = function () {
return this.each(function () {
var $this = $(this);
$(document).on('click', '.rockon', function () {
});
});
};
})(jQuery);
我将插件初始化为
$(".rockon").rockon();
但是,不要听" .rockon"点击一下,我怎么能听到我附加插件的元素。
我通常会这样做
$this.attr("class");
但该元素有多个类
答案 0 :(得分:2)
(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");
});
});
};