html页面

<body>
<div class='show_bg' style=" display:none;background:#000; color:#fff; text-align:center; height:40px; width:70px; font-size:13px;line-height:40px; border-radius:5px;box-shadow:0 0 7px #000; position:absolute; top:50%; left:50%; margin-left:-50px; z-index:999999">
	收藏成功
</div>


<div class="pull-left product_footer_3" onclick="shoucang(319)" style="width:100%">
                            
                            <div>
                            	<div id="shoucang_img"><img src="./Addons/WeiSite/View/default/TemplateIndex/index/img/shouchang_ings.png" width="25"></div>
                            	<div><span id="shoucangs">收藏</span></div>
                            </div>
                                            </div>


JS文件

<script>

var flag ="{:$flag}";
function shoucang(shopid){
        if(flag==1){
            $.ajax({ 
                    type:'post',
                    url: "{:U('insert_collection')}", 
                    data:'id='+shopid,
                    success: function(mag){
                            if(mag){
                                    $('.show_bg').show()    //显示
				    $('.show_bg').delay(1000).hide(0); //隐藏
                                    $("#shoucang_img img").attr("src","{:CUSTOM_TEMPLATE_PATH}Index/index/img/shouchang_ings.png");
                                    $("#shoucangs").text("收藏")   //取消收藏
                                    flag =0;
                                    return false;
                            }else{
                                    alert('收藏失败');
                            }
                    }

              });
      }
      
        if(flag==0){
            $.ajax({ 
                    type:'post',
                    url: "{:U('insert_collection')}", 
                    data:'del_id='+shopid,
                    success: function(mag){
                            if(mag){
                                    alert('已取消收藏');
                                    $("#shoucang_img img").attr("src","{:CUSTOM_TEMPLATE_PATH}Index/index/img/shouchang_ing.png");
                                    $("#shoucangs").text("收藏")
                                    flag =1;
                                    return false;

                            }else{
                                    alert('取消收藏失败');
                            }
                    }

              });
      }      
      
}

</script>




本文转载:CSDN博客