且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在锚点标记中以编程方式调用onclick()事件,同时在onclick函数中保留“this”引用?

更新时间:2023-10-28 20:08:46

你需要 apply 上下文中的事件处理程序那个元素:

You need to apply the event handler in the context of that element:

var elem = document.getElementById("linkid");
if (typeof elem.onclick == "function") {
    elem.onclick.apply(elem);
}

否则会引用上面代码执行的上下文。

Otherwise this would reference the context the above code is executed in.