jQuery click function vs inline onclick

前端 未结 5 1840
广开言路
广开言路 2021-02-08 11:28

I\'m trying to reorganize my code and make it more unobstrusive.

Mainly, there is a link for performing an action. If you click on the link, the action is performed via

5条回答
  •  清歌不尽
    2021-02-08 12:18

    The click handler isn't being picked up when you append the element to the DOM. Try using jQuery to register the click handler by changing the line(s) that look like:

    $("#" + placeId + " .actions").append("Added! Undo")
    

    To

    $("#" + placeId + " .actions").append("Added! Undo");
    $('#' + placeId + " .actions").find("span:last").find("a").click( function() {
        undoAddPlace( placeId );
        return false;
    });
    

    You might be able to do it more simply, but it looks like you could be adding more than one span to the paragraph so I went conservative. You could also probably chain off append, but I thought the reselect made the point clearer.

提交回复
热议问题