ITeye博客

JQuery -- this 和 $(this) 的区别

xxtianxiaxing · 2012-03-26 15:56 · JQuery · Web前端 · Markdown

<script type="text/javascript">
$(document).ready(function(){
	$("div").each(function(){
		alert($(this).html() + "----" + this.tagName + "--" + $(this)[0].id);
		alert((this == $(this)[0]));
		$(this).attr("title", $(this).html());
	});
});
</script>
</head>
<body>
<div id="node1">sss1</div>
<div id="node2">sss2</div>
</body>

 

#这里的this其实是一个Html元素(div),获取html属性值均可用this.属性名来取值
#这里的$(this)是一个JQuery对象,可以调用JQuery的方法
# this == $(this)[0] 返回为true
#JQuery拥有attr()方法可以get/set DOM对象的属性

评论(2)

  1. #2 楼 · xxtianxiaxing · 2012-04-05 23:10

    :idea:

  2. #1 楼 · zhangyaochun · 2012-03-26 19:16

    $(this)其实就是把this对象转成jQuery对象 $(this)就是又转成Element对象

评论功能维护中,当前仅展示历史评论。