How do I get the clicked element with inline HTML onclick and jQuery?

后端 未结 2 898
清酒与你
清酒与你 2021-01-21 05:18

I\'m creating a tags with this code:

$(\'#td\' + id).append(\'

相关标签:
2条回答
  • 2021-01-21 05:45

    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 讨论(0)
  • 2021-01-21 05:53

    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.

    0 讨论(0)
提交回复
热议问题