供调用者参考的 jQuery Event.Target

发布于 2024-09-25 18:52:22 字数 461 浏览 2 评论 0原文

我不知道我是否忘记了如何执行此操作,或者它是否是一个错误,但我只是无法使用 jQuery 在“click”事件中找到调用者的引用。

我正在执行以下操作:


$(document).ready(function() {
    $('#parent a.item').click(doSomething);
});

function doSomething(e) {
    // Alerts for demostrational purposes only
    alert(e.target);
    alert(e.currentTarget);
    alert(this);
    alert($(this)[0]);
}

所有警报都显示超链接的 href 属性(页面 URL + '#')。
我做错了什么吗?

注释: 使用 jQuery 1.4.2。

I don't know if I have forgotten how to do so or if it's a bug, but I just can't find the caller's reference in the "click" event using jQuery.

I'm doing the following:


$(document).ready(function() {
    $('#parent a.item').click(doSomething);
});

function doSomething(e) {
    // Alerts for demostrational purposes only
    alert(e.target);
    alert(e.currentTarget);
    alert(this);
    alert($(this)[0]);
}

All the alerts show the hyperlink´s href attribute(page URL + '#').
Am I doing something wrong?

Notes:
Using jQuery 1.4.2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春庭雪 2024-10-02 18:52:22

这是因为您正在发出警报,所以您会看到字符串表示形式(因为 alert() 接受一个字符串)...对于锚点来说,它是 href。例如,您可以这样做:

alert(e.target); //or perhaps alert(this.target); - alerts the href
alert(e.target.innerHTML);  //or perhaps alert(this.innerHTML); - alerts the html

您可以在此处尝试/使用它,请注意thise.target 并不总是相同,如果点击来自子元素,它们将会不同。

It's because you're alerting so you're seeing the string representation (since alert() takes a string)...which for an anchor is the href. You could do this for example:

alert(e.target); //or perhaps alert(this.target); - alerts the href
alert(e.target.innerHTML);  //or perhaps alert(this.innerHTML); - alerts the html

You can try it out/play with it here, note that this and e.target aren't always the same, if the click came from a child element, they'll be different.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文