0

我有以下代码:

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');
            }
        }
    });
});

我的期望是,当您单击 divresultLike时,它将执行function(). 但是,它什么也没做。我有两个alert()电话在那里,都没有被叫到。的输出console.log()如下:

[
<div class=​"resultLike searchBarGameLike">​&lt;/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
                            }
                        });
                    }
                );  
            }
        }
    }
);
4

2 回答 2

3

尝试

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

div.resultLike如果动态添加,您的实现可能不会受到约束

于 2012-08-06T12:05:54.983 回答
0

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

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

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

我不是 100% 为什么,但我认为这实际上改变了单击按钮时的位置。这意味着一旦它处于活动状态,它就不是你的鼠标所在的地方。我可能是错的,但这解决了它。

于 2012-08-06T13:30:23.540 回答