簡體   English   中英

如果沒有點擊按鈕,如何運行功能?

[英]How to run a function if a button is not clicked?

$("#RunCode").click(function(){
    var $this = $(this);
    if($this.data('clicked')) {
        console.log("Is clicked");
        $(".documentWrite").text("Is clicked");
    } else {
        $this.data('clicked', true);
        console.log("Is not clicked");
        $(".documentWrite").text("Is NOT clicked");
    }
});

的jsfiddle

當我點擊按鈕時,我得到“未點擊” ,如果我再次點擊,我會“點擊”
單擊按鈕時為什么要獲取unlick的代碼?

引用OP:

“當點擊按鈕時,為什么我會得到unlick的代碼?”

因為你的條件邏輯是有缺陷的。

$("#RunCode").click(function(){
    var $this = $(this);
    if($this.data('clicked')) { //<- FAILS on first click. 'data' is not yet set.
        console.log("Is clicked");
        $(".documentWrite").text("Is clicked");
    } else {
        $this.data('clicked', true);
        console.log("Is not clicked");
        $(".documentWrite").text("Is NOT clicked");
    }
});

第一次點擊時,稱為'clicked'datatrue ; 您只需在第一次單擊時設置true


引用OP:

“如果沒有點擊按鈕,如何運行功能?”

這甚至意味着什么? 您呈現給我們的代碼完全包含在click事件處理函數中。 換句話說,該函數僅取決於實際發生的單擊事件。 (它無法在沒有點擊的情況下運行)

如果您不想在單擊時運行該功能,那么如何?

暫無
暫無

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

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