J查询 .Ajax的悬停问题

Jquery .Hover issue with Ajax

本文关键字:悬停 问题 Ajax 查询      更新时间:2023-09-26

我有这个代码:

cuts_html = '';
for( i = 0; i < attachments.length; i++ ) {
    fburl3 = site_url + '/haircut-detail/?img_id=' + attachments[i].ID + '&uid=' + attachments[i].post_author;
    if( isNaN(attachments[i].view_count) ) attachments[i].view_count = 0;
        
    cuts_html += '<div id="controller-image" class="cut-image">';

    cuts_html += '<div id="cut-imageplacer">';
        
                            
    cuts_html +=    '<div class="cut-image-info">' +
                        'By <a href="' + user_profile_url + 
                        '&user_id=' + attachments[i].post_author + '">' +
                        attachments[i].author_name + '</a>' +
                        '</div>';
                            
    cuts_html +=    '<div class="commentbox-1">' +
                        '<img src="https://d2xcq4qphg1ge9.cloudfront.net/assets/17276/435546/original_views.png">&nbsp;&nbsp;' +
                        attachments[i].view_count + 
                        '&nbsp;&nbsp;&nbsp;&nbsp;<img src="https://d2xcq4qphg1ge9.cloudfront.net/assets/17276/435545/original_talk-bubble.png">&nbsp;&nbsp;' +
                        '<fb:comments-count href="' + fburl3 + '"></fb:comments-count>' +
                        '</div></div>';
    cuts_html +=    '<a class="cut-image-a" ' +
                        'href="' + image_detail_url + 
                        '?uid=' + attachments[i].post_author + 
                        '&img_id=' + attachments[i].ID + '">' +
                        attachments[i].attachment_image_html + '</a>';
                            
    cuts_html += '</div>';
}

Div Id cut-imageplacerdisplay:none,所以我想要的是当有人悬停在controller-imagediv上时,当然,在未打开时显示和隐藏cut-imageplacer。我使用此代码:

<script type="text/javascript">$(document).ready(function(){    $('.cut-image').hover(
    function(){     $('#cut-imageplacer').show();
}, function () {        $('#cut-imageplacer').hide();    });  }); </script>

但它不起作用...知道我做错了什么吗?还是指出我正确的方向?

看起来您正在尝试将事件附加到正在动态创建的元素。创建这些元素后,需要使用 Live()On() 将事件附加到这些元素。看看这个和这个,看看如何使用它们。

喜欢:

<script type="text/javascript">
      $(document).ready(function(){    
       $('.cut-image').live("hover",
          function(){     
              $('#cut-imageplacer').show();
          }, function () {        
              $('#cut-imageplacer').hide();    
           });  
       }); </script>