1. 程式人生 > >手機端長按 刪除 前後端 (settimeout代入函式和引數的方法)

手機端長按 刪除 前後端 (settimeout代入函式和引數的方法)

html:結構可以是

{loop $lists $k $v}
    <a href="?mid={$mid}&action=show&itemid={$v[itemid]}" class="rfq-item touchArea">
        <div class="rfq-item-top">
            <div class="customize">
                customized
            </div>
            <div class="rfq-item-title">
                {$v['title']}
            </div>
            <div class="release">{date("Y/m/d",$v['totime'])}</div>
        </div>
        <div class="rfq-item-cont">
            <div class="approved">
            {if $v['status']=='3'}
            Approved
            {elseif $v['status']=='2'}
            Waiting Approvel
            {elseif $v['status']=='1'}
            Not Pass
            {elseif $v['status']=='4'}
            Expired
            {/if}
            </div> <!-- Expired No Pass Audit -->
        </div>
    </a>
{/loop}

js指令碼
    var timeOutEvent=0;
    $(function(){
        $(".touchArea").on({
            touchstart: function(e){
                var _this = $(this);
                timeOutEvent = setTimeout(function(){longPress(_this);},1000);

            },
            touchmove: function(){
                clearTimeout(timeOutEvent);
                timeOutEvent = 0;
            },
            touchend: function(){
                clearTimeout(timeOutEvent);
                // if(timeOutEvent!=0){alert('這是單擊')}
            }
        })

        function longPress(_this){
            timeOutEvent = 0;
            var thisitem=_this.attr('href');
            thisitem=thisitem.replace('action=show','action=delete');
            if (confirm('You sure you want to delete? This will irrevocably')) {
                    var fdata=thisitem.substr(1);
                    $.ajax({
                        url:'#',
                        dataType:'json',
                        type:'POST',
                        data:fdata,
                        success: function(data) {
                            _this.remove();
                        }
                    });
            }
        }
    });