jQuery this揭秘

 jQuery this揭秘

this是什么?


In many object-oriented programming languages, this (or self) is a keyword which can be used in instance methods to refer to the object on which the currently executing method has been invoked. 

在面向对象语言中,this(或self)被用于在实例方法中指向调用该方法的对象。


jQuery的this

There are really two main contexts of 'this' in jQuery. The first refers to a to a  DOM  element, and the second to a jQuery object. 

在jQuery中this存在两种不同的解释。第一种指向一个DOM元素,第二种指向一个jQuery对象。

Example of this as a DOM element


将this作为DOM元素的实例

'this' is a DOM element when you are inside of a callback function (in the context of jQuery), for example, being called by the click, each, bind, etc. methods.


this在jQuery的回调函数,如click,each, bind等方法时是一个DOM元素。


$('a.newTarget').each(function() { // <- our anonymous callback
  // check the DOM attribute 'host' on this
  if (this.host != window.location.host) {
    // create a jQuery object using the current DOM element
    $(this).attr('target', '_new');
  }
});


Example of this as a jQuery object


将this作为jQuery对象的实例 


'this' is a jQuery object when you are inside your own jQuery functions. Note that the result of a selector query (i.e. $('a') ) is a jQuery object, which is an array of the matched DOM elements(imagine jQuery is an array with bells on). 


当this用于你自己的jQuery对象时,this是一个jQuery对象。

注: 一个选择器查询,如$('a')的结果是一个jQuery对象,其实是由一组DOM元素组成的数组。


jQuery.fn.newTarget = function() {
  // 'this' is a jQuery object at this point - with all the jQuery functions

  return this.each(function() { // return so we don't break the chain
    // now we are inside of a jQuery function, the DOM element is the context
    // so 'this' has changed to become a DOM element.

    if (this.host != window.location.host) {
      $(this).attr('target', '_new');
    }
  });
};


Finishing up

结束语


This is far from comprehensive, but equally there's very little to the logic. So long as you remember the context of 'this' changes when moving in and out of object methods then you're on your way. 


以上远不足以使你理解jQuery中this的用法。但是只要当你move int and out of 对象时谨记this改变的上下文,then you're on your way.


If you're still not sure, get your hands on Firebug and add 'console.log(this)' within your code to interrogate and understand what 'this' is at that point in your code. 


如果你仍不确信,使用Firebug,添加‘console.log(this)’到你的代码中来查询并理解当前位置的this的含义。

### 回答1: JQuery中的this表示当前被选中的元素,在使用JQuery方法时会基于这个元素进行操作。如果要针对this的子元素进行操作,可以使用JQuery提供的多个方法来选择并操作子元素。 首先,可以使用find()方法来选择this的子元素。例如,如果this是一个父元素,我们可以使用this.find("子元素选择器")来选择所有子元素,然后对它们进行操作。 另外,可以使用children()方法选择this的直接子元素。这个方法只会选取this的直接下级元素,而不会选择更深层次的子元素。 此外,还可以使用siblings()方法来选择this的同级元素,这些元素具有与this相同的父元素。这个方法可以用于选择和操作其他的兄弟元素。 还有一个常用的方法是next()和prev(),分别用于选择this的下一个兄弟元素和上一个兄弟元素。这些方法很有用,特别是在需要对this的相邻元素进行操作时。 另外,如果需要选择this的第一个子元素或最后一个子元素,可以使用:first-child和:last-child选择器。例如,this.find(":first-child")将选择第一个子元素,this.find(":last-child")将选择最后一个子元素。 总之,JQuery中的this可以通过使用不同的方法和选择器来选择和操作其子元素,包括find()、children()、siblings()、next()、prev()等等。这些方法可以根据需要选择合适的子元素,然后对它们进行各种操作。 ### 回答2: jQuery中的this是指当前选中的元素,而其子元素可以通过this来进行遍历和操作。 首先,通过jQuery选择器选中元素后,this就表示当前选中的元素。我们可以使用this来获取该元素的子元素。 例如,假设我们有一个ul列表: <ul> <li>第一个元素</li> <li>第二个元素</li> <li>第三个元素</li> </ul> 我们可以使用jQuery选择器选中ul,并使用this来获取其子元素li: $("ul").each(function(){ $(this).children("li").each(function(){ // 这里的this表示当前选中的li元素 console.log($(this).text()); }); }); 上述代码中,我们先使用each()方法遍历选中的ul元素,通过children()方法获取其子元素li,然后再使用each()方法遍历li元素。在li元素的遍历中,this表示当前选中的li元素,我们可以通过$(this)来操作和获取li元素的属性和内容。 总结起来,通过this可以获取当前选中元素的子元素,进而实现对子元素的遍历和操作。同时,也可以通过this来获取子元素的父元素,进行父子元素之间的交互。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值