【发布时间】:2019-01-06 06:16:45
【问题描述】:
我理解事件处理程序中$(this) 的含义。
$("div#d1").on("mouseover", function() {
$(this).attr("id", "d2");
alert("id: " + $(this).attr("id"));
});
但是,当它用在 jQuery 代码的顶层时,这是什么意思呢?
var tgt = $("h2#slider");
tgt.hide();
tgt.slideDown(2000);
$(this).on("click", function() {
tgt.slideUp();
});
【问题讨论】:
-
this在根级别将引用windowobject。将其包装在$(...)中将其返回为 jQuery object。相关文档链接。
标签: jquery html dom this element