---
title: "不要滥用jQuery的$(this)"
author: "my14686"
date: "2011-11-11T23:09:23Z"
tags: ["DOM", "Java", "click", "jQuery"]
categories: ["Web前端"]
slug: "jquery-this"
draft: false
lastmod: "2011-11-11T23:09:23Z"
id: 1254677
url: "https://my14686.iteye.com/blog/1254677"
---

如果不了解javasrcipt中基本的DOM属性和方法的话，很容易滥用jQuery对象。比如： $('#someAnchor').click(function() { alert( $(this).attr('id') ); }); 如果你只是通过jQ对象获取简单的dom元素的属性比如id，那么你完全可以使用js原生的方法： $('#someAnchor').click(function() { alert( this.id ); }); 诸如“src,” “href,” 和“style.”等一些属性在...
如果不了解javasrcipt中基本的DOM属性和方法的话，很容易滥用jQuery对象。比如：
$(‘#someAnchor’).click(function() {
alert( $(this).attr(‘id’) );
});
如果你只是通过jQ对象获取简单的dom元素的属性比如id，那么你完全可以使用js原生的方法：
$(‘#someAnchor’).click(function() {
alert( this.id );
});
诸如“src,” “href,” 和“style.”等一些属性在老版本的ie中使用了getAttribute方法