簡體   English   中英

如何在表TD上禁用和啟用單擊事件?

[英]How to disable and enable a click event on a table td?

我有一個html表。 我使這張表中的1噸可點擊。 如果用戶單擊,它將在一定時間間隔內在屏幕上打印一些內容。 因此,此打印需要時間。 我想在打印期間禁用單擊事件。 打印完成后,然后應再次單擊事件。 以下是我的代碼:

var html = "<table class='analysis_table'>"
    +"<tr>"
        +"<th>Mismatch</th>"
        +"<th>Gap (%)</th>"
    +"</tr>"
    +"<tr>"
        +"<td class='clickMM'>"+mismatch+"</td>"
        +"<td>"+gap+"</td>"
    +"</tr>"
+"</table>";

$("#analysis").html(html);

$(document).on("click", ".clickMM", function () {
    $(this).css("pointer-events", "none");
    show(interval)
        .then(function(result){
            if(result == 'done')
                $(this).css("cursor", "pointer");   
        })
        .catch(function notOk(err) {
            console.error(err)
        })
});
function show(interval) {

    var promise = new Promise(function(resolve, reject) {
        printError();
        for(var i=0;i<(Order.length)-1;i++)
        {   printByTime(interval);//this includes setTimeout function
        }
        resolve("done");
        reject("Error");
    });
    return promise;
}

它停止單擊,但不再繼續。 這有什么問題?

如前所述,創建一個CSS類

.ClickMe {            
}

然后將類添加到TD列

 <td class="ClickMe">

然后使用該類作為選擇器捕獲單擊,並將指針事件設置為none。

 $(document).on("click", ".ClickMe", function () {
        alert("click");
        $(this).css("pointer-events", "none");
 });

再次單擊將不再顯示警報。 我假定無論調用什么機​​制進行打印,都可以在返回時重置指針事件。

謝謝

創建您想要單擊時發生的功能,並在需要時綁定和取消綁定該功能。

這是一個測試樣本

var tester=function(e){
    e.preventDefault();
    $(".clickMM").css('color','#f00');
    console.log('tester fired');
    //REMOVE CLICK AND DO YOUR TIMED STUFF HERE
    $(".clickMM").unbind("click");
    timer();
}
var timer=function(){
    //DO YOUR TIMED STUFF HERE
    setTimeout(function(){
        $(".clickMM").css('color','#000');
        //ON COMPLETE REBIND USING THIS
        $(".clickMM").bind("click",tester);
    },3000);
}

//ADD THE FUNCTION ON CLICK
$(".clickMM").on("click",tester);

這樣不夠好嗎?

$("#mm").click(function(){
    if($(this).attr("name") == "off"){
        $(this).attr("name","on"); // so nothing happens when the user clicks now
        showMismatch(); //prints something on screen with some time interval
        $(this).attr("name","off");  
}   
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM