相关疑难解决方法(0)

'$(this)'和'this'之间有什么区别?

我目前正在学习本教程:jQuery入门

对于以下两个示例:

$("#orderedlist").find("li").each(function (i) {
    $(this).append(" BAM! " + i);
});
$("#reset").click(function () {
    $("form").each(function () {
        this.reset();
    });
});
Run Code Online (Sandbox Code Playgroud)

请注意,在第一个示例中,我们使用$(this)在每个li元素中附加一些文本.在第二个示例中,我们this在重置表单时直接使用.

$(this)似乎比使用频率更高this.

我的猜测是在第一个例子中,$()将每个li元素转换为理解append()函数的jQuery对象,而在第二个示例中reset()可以直接在表单上调用.

基本上我们需要$()特殊的jQuery功能.

它是否正确?

javascript jquery this

560
推荐指数
6
解决办法
58万
查看次数

在jquery中函数.fn的return语句中的"this"?

为什么在这个函数中返回"this":

(function($) {

  $.fn.menumaker = function(options) {

      var cssmenu = $(this), settings = $.extend({
        title: "Menu",
        format: "dropdown",
        sticky: false
      }, options);

      return this.each(function() {
Run Code Online (Sandbox Code Playgroud)

this是"DOM元素"?如果我们想要使用函数的内容,为什么不引用$(this)使用我们所针对的元素,而不是this

谢谢

javascript jquery this

1
推荐指数
1
解决办法
59
查看次数

标签 统计

javascript ×2

jquery ×2

this ×2