---
title: "jQuery中this与$(this)的区别"
author: "Ueaner"
date: "2010-05-27T15:23:58Z"
tags: ["HTML", "JavaScript", "Web", "jQuery", "浏览器"]
categories: ["Web前端"]
slug: "jquery-this-this"
draft: false
lastmod: "2010-05-27T15:24:29Z"
id: 676896
url: "https://ueaner.iteye.com/blog/676896"
---

jQuery中this与$(this)的区别

```javascript

$("#textbox").hover(
	function() {
		this.title = "Test";
	},
	fucntion() {
		this.title = "OK";
	}
);
```

这里的this其实是一个Html 元素(textbox)，textbox有text属性，所以这样写是完全没有什么问题的。
但是如果将 this换成$(this)就不是那回事了，Error--报了。this与$(this)的区别在此。

```Error

$("#textbox").hover(
	function() {
		$(this).title ＝ "Test";
	},
	function() {
		$(this).title = "OK";
	}
);
```

这里的$(this)是一个JQuery对象，而jQuery对象沒有title 属性，因此这样写是错误的。

JQuery拥有attr()方法可以get/set DOM对象的属性，所以正确的写法应该是这样：

```Right
$("#textbox").hover(
	function() {
		$(this).attr('title','Test');
	},
	function() {
		$(this).attr('title','OK');
	}
);
```

使用jQuery的好处是它包裝了各种浏览器版本对DOM对象的操作，因此统一使用$(this)而不再用this应该是比较不错的选择。

来自：http://sys.firnow.com/web/x8002010n03m/15s9019552.html