最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I get the clicked element with inline HTML onclick and jQuery? - Stack Overflow

programmeradmin3浏览0评论

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 function

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

    alert($(this));
}

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

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 function

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

    alert($(this));
}

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

Share Improve this question asked May 24, 2010 at 18:01 ThomasThomas 411 silver badge2 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 13

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>

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

You need to split it, like this:

$("#td" + id).append("<p><a  href=\"#\" /></p>");

and then select the new element

$("#td > p > a").click(
function(){
//this now will be pointing to the selected element
}
)
发布评论

评论列表(0)

  1. 暂无评论