2014-02-17 41 views
0

我不能選擇元素,改變內容在它 這裏是我的html爲什麼我不能選擇在jQuery的元素和改變內容

<div class="like_user_wrapper" id="<?php echo $post_id.'like_user_wrapper' ;?> "> 
hello, world 
</div> 

<span class="post_bottom_bar"> 
<img class="thumb_icon" onmouseover=" 

var post_id=<?php echo $post_id ?>; 
$.ajax({ 
url:'ajax_like_user.php', 
method:'post', 
data:{post_id:post_id}, 
success:function(data){ 

$('<?php echo '#'.$post_id.'like_user_wrapper' ?>').html('hello'); 

} 

}) 
" > 

我不能把它變成打招呼,當我的將鼠標放在圖標上

+2

*不要*使用內聯事件!在JavaScript中正確綁定事件*。 '$('。thumb_icon')。mouseover(function(){})' –

回答

3

您在id =「」部分中有額外的空間。將其更改爲:

id="<?php echo $post_id.'like_user_wrapper' ;?>"> 

注意;?>之後的空間已被刪除。

+0

哦,天哪,我甚至不知道這件事會影響選擇。不過謝謝,無論如何! –

0

你可以這樣做:

<img class="thumb_icon" /> 
<script> 
     $(".thumb_icon").mouseover(function(){ 
     var post_id=<?php echo $post_id ?>; 
     $.ajax({ 
     url:'ajax_like_user.php', 
     method:'post', 
     data:{post_id:post_id}, 
     success:function(data){ 
     $('<?php echo '#'.$post_id.'like_user_wrapper' ?>').html('hello'); 

     }   

     })     
     }) 
    </script> 

<img class="thumb_icon" onmouseover="mouseoverImg();" /> 
<script> 
     function mouseoverImg(){ 
     var post_id=<?php echo $post_id ?>; 
     $.ajax({ 
     url:'ajax_like_user.php', 
     method:'post', 
     data:{post_id:post_id}, 
     success:function(data){ 
     $('<?php echo '#'.$post_id.'like_user_wrapper' ?>').html('hello'); 

     }   

     })     
     } 
    </script> 
0

我認爲你需要做這樣的事情:

HTML:

<div class="like_user_wrapper" id="<?php echo $post_id.'like_user_wrapper';?>"> 
hello, world 
</div> 

<span class="post_bottom_bar"> 
<img class="thumb_icon" id="<?php echo $post_id;?>" > 
</span> 

的Javascript:

$(document).ready(function(){ 

    $('.thumb_icon').mouseover(function(){ 
     var post_id = $(this).attr("id"); 

     $.ajax({ 
     url:'ajax_like_user.php', 
     method:'post', 
     data:{post_id:post_id}, 
     success:function(data){ 

     $('#' + post_id + 'like_user_wrapper').html('hello'); 

     } 

     }); 

    }); 

}); 
相關問題