jQuery click function vs inline onclick

前端 未结 5 1845
广开言路
广开言路 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:21

    Wow!! Thanks to all of you!

    I've read about the live event and final working code is:

    $(function() {      
                $(".add2").click(function(){
                    var placeId = $(this).parents(".place").attr("id");
                    $("#" + placeId + " .add2").hide();
                    $("#" + placeId + " .actions").append("Added! Undo");
                    return false;
                })
                $('.undoadd2').live('click', function(){ 
                    var placeId = $(this).parents(".place").attr("id");
                    $("#" + placeId + " .actions").find("span.added, a.undoadd2").remove();
                    $("#" + placeId + " .add2").show();
                    return false;
                });
    

    Previously I used remove() for delete the text that offers to perform the action. I've changed it for hide/show so I don't have to use live also in the first function.

    Thanks again!

提交回复
热议问题