如何使用内联HTML onclick和jQuery获得clicked元素? [英] How do I get the clicked element with inline HTML onclick and jQuery?

查看:91
本文介绍了如何使用内联HTML onclick和jQuery获得clicked元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码创建标签:

I'm creating a tags with this code:

$('#td' + id).append('<p><a href="#" onclick="excluirArquivo(\'' + response + '\'); return false;"><img src="/erp/proposta/media/images/delete.png" alt="Excluir arquivo" /></a> ' + file + '</p>');

excluirArquivo函数

excluirArquivo function

function excluirArquivo(arquivo) {
    $.ajax({
        type: 'POST',
        url: '/erp/proposta/index.php/arquivo/remover/' + arquivo
    });

    alert($(this));
}

但是excluirArquivo函数中的此元素正在返回Window对象.如何获得excluirArquivo内部的clicked元素(标签)?

But this element inside excluirArquivo function is returning the Window object. How do I get the clicked element (a tag) inside of excluirArquivo?

推荐答案

如果您必须以这种方式(即"DOM 0"方式,而不是jQuery)分配事件处理程序,您可以这样做:

If you must assign your event handler that way (that is, the "DOM 0" way, instead of with jQuery), you can do this:

<a href='#' onclick='excluirArquivo(this)' > ... </a>

或者,我想(假设您要传递参数):

Or, I suppose (given that you want to pass a parameter):

<a href='#' onclick='excluirArquivo.call(this, param)'> ... </a>

那样,函数中的"this"将成为元素,看起来更接近您想要的元素.

That way, "this" inside the function will be the element, which seems closer to what you want.

这篇关于如何使用内联HTML onclick和jQuery获得clicked元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆