简体   繁体   English

jQuery Event.Target问题

[英]jQuery Event.Target Problem

I don´t know if I have forgotten how to do so or if it´sa bug, but I just can´t find the caller's reference in the "click" event using jQuery. 我不知道我是否忘记了该怎么做或它是否是一个错误,但是我只是无法使用jQuery在“ click”事件中找到调用者的引用。

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 + '#'). 所有警报均显示超链接的href属性(页面URL +'#')。
Am I doing something wrong? 难道我做错了什么?

Notes: Using jQuery 1.4.2. 注意:使用jQuery 1.4.2。

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 . 这是因为您正在发出警报,所以您看到的是字符串表示形式(因为alert()需要一个字符串)...对于锚点而言,它是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. 您可以在此处尝试/使用它 ,请注意, thise.target并不总是相同的,如果点击来自子元素,它们将有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM