我的onclick链接有一个属性:
<a href="#" onclick="myFunc(1,2,3)">click</a>
Run Code Online (Sandbox Code Playgroud)
这指向JavaScript中的这个事件处理程序:
function myFunc(p1,p2,p3) {
//need to refer to the current event object:
alert(evt.type);
}
Run Code Online (Sandbox Code Playgroud)
由于事件对象"evt"未传递给参数,是否仍然可以获取此对象?
我试过window.event和$(window.event),但两者都是undefined.
任何的想法?
我将如何使用JavaScript获取锚标记的文本.我知道如何通过在标签上附加一个ID来做到这一点,但我想知道是否还有使用"this"关键字来做到这一点.
示例HTML代码段:
<ul>
<li><a href="javascript:alertText(this);">Link One</a></li>
<li><a href="javascript:alertText(this);">Link Two</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
JavaScript功能:
function alertText(callingElement) {
alert(callingElement.text);
}
Run Code Online (Sandbox Code Playgroud)
这不起作用(警报打印出"未定义")因为"this"关键字似乎指向Window对象而不是调用该函数的锚.
如果有必要,可以使用JQuery.