【发布时间】:2011-06-26 13:17:10
【问题描述】:
可能重复:
What's the difference between $(this) and this in jQuery?
在jquery selector中,示例代码为:
<body>
<select name="garden" multiple="multiple">
<option>Flowers</option>
<option selected="selected">Shrubs</option>
<option>Trees</option>
<option selected="selected">Bushes</option>
<option>Grass</option>
<option>Dirt</option>
</select>
<div></div>
<script>
$("select").change(function () {
var str = "";
$("select option:selected").each(function () {
str += $(this).text() + " "; // I interested it this line
});
$("div").text(str);
})
.trigger('change');
</script>
</body>
在示例代码中,有一段代码:
str += $(this).text() + " ";
我在想,为什么这里不用str += this.text() + " ";?换句话说,为什么不在这部分代码中使用this 而使用$(this)?在这种情况下this 和$(this) 有什么区别?
【问题讨论】:
标签: jquery jquery-ui jquery-selectors