我想用一个jQuery专家来解释为什么$(文档)标识符被其他人推荐用于jQuery的on()语句而不仅仅是使用元素本身
示例1:为什么在这里使用$(document)比Example#2更好?
$(document).on("click", ".areaCodeList a", function(){
// do stuff
});
Run Code Online (Sandbox Code Playgroud)
示例2:与示例1相比,为什么使用这种方式考虑不好的做法?
$(".areaCodeList a").on("click", function(){
// do stuff
});
Run Code Online (Sandbox Code Playgroud) 我的对象/功能范围和对某些事物的一般理解有问题.我正在调用doc中的jQuery函数,如下所示:
$("#interactiveQA .actionTile").on('click',function(){
$(this).activeTiles("init");
});
Run Code Online (Sandbox Code Playgroud)
然后我在这里创建脚本:
(function($) {
var methods = {
init: function() {
var tileClicked = $(this).attr('id');
}
};
$.fn.activeTiles = function(method) {
// Method calling logic
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.slider');
}
};
})(jQuery);
Run Code Online (Sandbox Code Playgroud)
这是我想要找到的.尽可能在技术上,你能解释为什么var tileClicked = $(this)attr('id')在当前位置不起作用吗?您能否请详细解释您将采取哪些不同的做法或最佳做法等?