我是Jquery和Js的新手。我需要帮助。我想显示图像Onclick而不是文本
echo '<a href="#" class="like" id="'.$id_post.'" title="Unlike"><img src="images/fav1.png" width="32"height="32"></a>';
}else {
echo '<a href="#" class="like" id="'.$id_post.'" title="Like"><img src="images/fav.jpg"width="32"height="32"></a>';
}
jquery的
$(document).ready(function(){
$(document).on('click', '.like', function(){
if($(this).attr('title') == 'Like'){
$that = $(this);
$.post('action.php', {pid:$(this).attr('id'), action:'like'},function(){
$that.text('Unlike');
$that.attr('title','Unlike');
});
}else{
if($(this).attr('title') == 'Unlike'){
$that = $(this);
$.post('action.php', {pid:$(this).attr('id'), action:'unlike'},function(){
$that.text('Like');
$that.attr('title','Like');
});
}
}
});
});
请帮助
答案 0 :(得分:1)
只需点击您对文字的处理方式,即可更改img
src。
if($(this).attr('title') == 'Like') {
$that.find("img").attr("src","images/fav1.png");
}
else if ($(this).attr('title') == 'Like') {
$that.find("img").attr("src","images/fav.jpg");
}