我需要在onclick事件的调用对象上有一个处理程序.

<a href="123.com" onclick="click123(event);">link</a>

<script>
  function click123(event) {
    //i need <a> so i can manipulate it with Jquery
  }
</script>

I want to do this without the use of $().click or $().live of jQuery but with the method described above.

推荐答案

pass in this in the inline click handler

<a href="123.com" onclick="click123(this);">link</a>

or use event.target in the function (according to the W3C DOM Level 2 Event model)

function click123(event)
{
    var a = event.target;
}

But of course, IE is different, so the vanilla JavaScript way of handling this is

function doSomething(e) {
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
}

或者说不那么冗长

function doSomething(e) {

    e = e || window.event;
    var targ = e.target || e.srcElement || e;
    if (targ.nodeType == 3) targ = targ.parentNode; // defeat Safari bug
}

其中e是在IE以外的浏览器中传递给函数的event object.

If you're using jQuery though, I would strongly encourage unobtrusive JavaScript and use jQuery to bind event handlers to elements.

Jquery相关问答推荐

将div中的下一个2个标题分组

如何在 jQuery Select 器中定义 css :hover 状态?

什么是不使用 jQuery 的经验技术原因?

jQuery ajax() 使用成功、错误和完成与 .done()、.fail() 和 always()

JavaScript 吸管(告诉鼠标光标下像素的 colored颜色 )

动态设置 iframe src

jquery click 不适用于 ajax 生成的内容

Rails,单击link_to helper后未加载javascript

moment.js isValid 函数无法正常工作

如何使用 jQuery UI Resizable 仅水平或垂直调整大小?

Zepto 和 jQuery 2 有什么区别?

如何使用 jquery 更改 onclick 事件?

如何将文本从 div 复制到剪贴板

如何使用 jquery 动态更改 iframe 中的内容?

使用 JQuery 激活 bootstrap 选项卡

jquery分别绑定双击和单击

如何使用 jQuery 取消设置元素的 CSS 属性?

如何使用 JQuery $.scrollTo() 函数滚动窗口

jQuery或javascript查找页面的内存使用情况

在 jquery 中使用 AJAX Post 从强类型 MVC3 视图传递模型的正确方法