jquery .on() is not triggering

jquery .on() is not triggering

本文关键字:not triggering is on jquery      更新时间:2023-09-26

我有以下代码:

console.log($(".resultLike"));
$(".resultLike").on("click", function (event) {
    alert('test');
    event.stopPropagation();
    alert('test1');
    value['clicked'] = 1;
    $.ajax({
        url: 'scripts/php/userProfile.php',
        data: {
            action: value
        },
        type: 'post',
        dataType: 'json',
        success: function (profileData) {
            if (profileData == 'removed') {
                $(me).children('.resultInside')
                .children('.resultData')
                .children('.resultLike').html('Favourite?');
            } else if (profileData == 'notLoggedIn') {
                $(me).children('.resultInside')
                .children('.resultData')
                .children('.resultLike').html('Login to add');
            } else {
                $(me).children('.resultInside')
                .children('.resultData')
                .children('.resultLike').html('un-Favourite');
            }
        }
    });
});

我的期望是,当您单击div resultLike时,它将预生成function()。然而,它什么也没做。我有两个alert()呼叫,但都没有被呼叫。console.log()的输出如下:

[
<div class=​"resultLike searchBarGameLike">​</div>​
] 

这证明了它被写在了纸上。任何帮助都将不胜感激,谢谢。

编辑:

我认为应该提到的是,我实际上使用了两个.on()事件。这实际上是我的全部代码。问题在于$("body").on("click", ".resultLike", function(){行,它不起作用。

$searchedGamesContainer.on(
    "click",
    ".box",
    function(event){
        if(!$displayLock){
            $displayLock = true;
            var description;
            var user_id;
            if($(this)[0].style.width == '75%'){
                var org_img = $(this).children(".resultInside").find("img").attr("src"); 
                $(this).children(".resultInside").append("<img src='"+org_img+"' id='imgId'/>");
                $(this).children(".resultInside").css('height','auto');
                $(this).css('width', '18%');
                $(this).css('height', 'auto');
                $(this).find(".resultData").fadeOut('fast');
                setTimeout(function(){$searchedGamesContainer.masonry('reload');},300);
                setTimeout(function(){$displayLock = false;},1000);
            }else{
                var me = this;
                var pos; 
                largeImage=  new Image();
                value['name']=$(me).find("p").html();
                oldImage = $(this).find("img").attr("src");
                for(var i = 0; i<$searchBarGames.length; i++){
                    if($searchBarGames[i][5] == value['name']){
                        pos = i; 
                        break
                    }
                }
                description = $searchBarGames[pos][2];
                $(me).find("img").hide();
                largeImage.src = $searchBarGames[pos][4];
                $(me).find("img").attr("src",largeImage.src);
                $(me).children(".resultInside").css('height','400px');
                $(me).css('width', '75%');
                $(me).children(".resultInside").html("'
                    <div class='resultData'>'
                        <div class='resultImg'><img src='"+ largeImage.src +"'></div>'
                        <div class='resultName'>" + value['name'] + "</div>'
                        <div class='resultDesc'>" + description +"</div>'
                        <div class='wikiLink searchBarGameWiki'><a href='http://wikipedia.org/wiki/" + value['name'] + "'>Wikipedia</a></div>'
                        <div class='resultLike searchBarGameLike'></div>'
                    </div>");
                value['clicked']=0;
                $.ajax({
                    url:'scripts/php/userProfile.php',
                    data:{action:value},
                    type: 'post',
                    dataType: 'json',
                    success:function(profileData){
                        //logic
                    }
                });     
                console.log($(".resultLike"));
                $("body").on("click", ".resultLike", function(){
                        alert('test');
                        event.stopPropagation();
                        alert('test1');
                        value['clicked']=1;
                        $.ajax({
                            url:'scripts/php/userProfile.php',
                            data:{action:value},
                            type: 'post',
                            dataType: 'json',
                            success:function(profileData){
                                //logic
                            }
                        });
                    }
                );  
            }
        }
    }
);

尝试

$("body").on("click", ".resultLike", function(){
      // your code goes here.
});

如果动态添加div.resultLike,则您的实现可能不会被绑定

这个问题最终完全是因为CSS。

对于我想要警报的div元素,我有一个看起来像按钮的标记
然而,我注意到了这一点:

.resultLike:active {
    position:relative;
    top:1px;
    z-index:1235;
}

我不是百分之百的原因,但我认为这实际上改变了点击按钮时按钮的位置。这意味着一旦它处于活动状态,它就不是你的鼠标所在的位置。我可能错了,但这解决了问题。