簡體   English   中英

刪除帶有超時的單擊時的Div

[英]Remove Div On Click With Timeout

我點擊此javascript時就刪除了div,但它根本不起作用:(

你能幫我么 ? 我會很高興(我已經嘗試搜索其他問題)

有JS

onclick="setTimeout('$('#wait').remove()', 11000);"

錯誤的語法和引號用法。 這個:

onclick = "setTimeout(function() { $('#wait').remove() }, 11000);";

是正確的。

注意處理程序中嵌套的單引號。 那不會很好地結束。 瀏覽器應該如何處理?

'$('#wait').remove()'

您確實想定義一個函數並使用 您避免了將字符串傳遞給setTimeout() ,多級引用等的所有陷阱。

function hideit() {
  $('#wait').remove();
}

// ...

<button onclick="setTimeout(hideit, 11000);">click me</button>

而不是在onclick .delay一些javascript,您應該使用jQuery中的.delay.queue

 $('#clickme').on('click', function(){ $('#wait').delay(11000).queue(function(){ $(this).remove().dequeue() }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="wait">Gone in 11 Seconds</div> <button id="clickme">Click me to start the countdown</button> 

我認為所有這些解決方案幾乎都可以使用。 Chymmi,這是您在那里所做的另一個也許更優雅的版本。

JsFiddle演示: https ://jsfiddle.net/kvvbbz6e/4/


使用Javascript

$(document).ready(function(){
    //--------------------------------------------------------------
    // this is what you would need
    var waitButton = $('#wait'),
        waitButtonTimer;

    waitButton.on('click',function(){ // clicking this a second time will reset the timer.
        clearInterval(waitButtonTimer);

        waitButtonTimer = setTimeout(function(){
            waitButton.off('click');
            $('.infolabel').text('click event unbound');
        }, 4000);

    });
    //--------------------------------------------------------------
});

HTML

<div id="wait" class="button">Wait Button</div>
<span class="infolabel">Click event bound</span>

CSS

.button {
    display: inline-block;
    color: #666;
    height: 24px;
    font-size: 9.5pt;
    font-weight: bold;
    line-height: 22px;
    border: 0;
    padding: 0 5px;
    margin: 0;
    border: 1px solid rgba(0, 0, 0, 0.2);
    border-radius: 4px;
    background: rgb(255,255,255); /* Old browsers */
    background: -moz-linear-gradient(top, rgba(255,255,255,1) 60%, rgba(245,245,245,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(60%,rgba(255,255,255,1)), color-stop(100%,rgba(245,245,245,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, rgba(255,255,255,1) 60%,rgba(245,245,245,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, rgba(255,255,255,1) 60%,rgba(245,245,245,1) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top, rgba(255,255,255,1) 60%,rgba(245,245,245,1) 100%); /* IE10+ */
    background: linear-gradient(to bottom, rgba(255,255,255,1) 60%,rgba(245,245,245,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#f5f5f5',GradientType=0 ); /* IE6-9 */
    box-shadow: 0px 1px 1px #fff;
    cursor: pointer;
    box-sizing: border-box;
    -moz-box-sizing: border-box;
}

暫無
暫無

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

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