我在对象/函数范围和对某些事物的一般理解方面遇到了问题。我正在准备好文档中调用 jQuery 函数,如下所示:
$("#interactiveQA .actionTile").on('click',function(){
$(this).activeTiles("init");
});
然后我像这样创建脚本:
(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);
这是我想要找出的内容。从技术上来说,您能否解释一下为什么 vartileClicked = $(this)attr('id') 在当前位置不起作用?您能否在技术细节上解释一下您会采取哪些不同的做法或最佳实践等?
最佳答案
打字错误:
$(this).attr('id');
注意。
关于javascript - jQuery 命名空间和使用 "this",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12160674/