---
title: "jQuery中$(this)和this 区别"
author: "IT_hack"
date: "2013-05-03T16:24:28Z"
tags: []
categories: ["Web前端"]
slug: "jquery-this-this"
draft: false
lastmod: "2013-06-08T14:30:23Z"
id: 1859396
url: "https://w3cking.iteye.com/blog/1859396"
---

最近经常用到JQuery，今天就和大家简单说所$(this)和this的区别  
jQuery中this与$(this)的区别：　this 指代的是 DOM 对象，而$(this)指代的是 jQuery 对象。 就相当于以下两者的区别：

1. var a=document.getElementsByTagName('a')\[0];
2. var $a=$('a').eq(0);

*复制代码*

看个例子：

01. &lt;body&gt;
02. &lt;a href="http://www.baidu.com/"&gt;&lt;/a&gt;
03. &lt;a href="http://www.cnblogs.com/duanhuajian/"&gt;&lt;/a&gt;
04. &lt;/body&gt;
05. &lt;script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"&gt;&lt;/script&gt;
06. &lt;script type="text/javascript"&gt;
07. $('a').each(function(){
08. console.log(this.href);
09. console.log($(this).href);
10. });
11. &lt;/script&gt;

*复制代码*

其结果如下图： &lt;ignore\_js\_op style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: Tahoma, Helvetica, SimSun, sans-serif; "&gt;![JQuery this](http://www.it-fans.com/data/attachment/forum/201305/03/162032kommeozamonej0on.jpg "JQuery this")   
因为$(this)没有href属性，所以会显示undefined;如果使用$(this),正确写法因该是：

1. $(this).attr('href');

*复制代码*