"this" in the return statement in a function .fn in jquery?
"this" in the return statement in a function .fn in jquery?
为什么在此函数中返回 "this":
(function($) {
$.fn.menumaker = function(options) {
var cssmenu = $(this), settings = $.extend({
title: "Menu",
format: "dropdown",
sticky: false
}, options);
return this.each(function() {
this
是 "DOM element" 吗?如果我们想使用函数的内容,为什么不引用 $(this)
来使用我们定位的元素,而不是 this
?
谢谢
jQuery 方法($.fn
的属性)不像 jQuery 事件处理程序回调。 this
的值是 jQuery 对象本身,而不是 DOM 元素。
$.fn
对象是 jQuery 个实例的原型对象,所以当你创建一个 jQuery 对象时
var jq = $(something);
然后调用一个jQuery方法:
jq.whatever();
JavaScript 的自然 this
规则决定 whatever()
将使用 jQuery 对象作为 this
的值调用。
为什么在此函数中返回 "this":
(function($) {
$.fn.menumaker = function(options) {
var cssmenu = $(this), settings = $.extend({
title: "Menu",
format: "dropdown",
sticky: false
}, options);
return this.each(function() {
this
是 "DOM element" 吗?如果我们想使用函数的内容,为什么不引用 $(this)
来使用我们定位的元素,而不是 this
?
谢谢
jQuery 方法($.fn
的属性)不像 jQuery 事件处理程序回调。 this
的值是 jQuery 对象本身,而不是 DOM 元素。
$.fn
对象是 jQuery 个实例的原型对象,所以当你创建一个 jQuery 对象时
var jq = $(something);
然后调用一个jQuery方法:
jq.whatever();
JavaScript 的自然 this
规则决定 whatever()
将使用 jQuery 对象作为 this
的值调用。