简体   繁体   English

删除带有超时的单击时的Div

[英]Remove Div On Click With Timeout

I have this javascript on click to remove div on click, but it doesnt work at all :( 我点击此javascript时就删除了div,但它根本不起作用:(

Can you please help me ? 你能帮我么 ? I would be so happy (I already tried to search over the other questions) 我会很高兴(我已经尝试搜索其他问题)

There is JS 有JS

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

Wrong syntax and quotes usage. 错误的语法和引号用法。 This: 这个:

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

would be correct. 是正确的。

Note the nested single quotes in your handler. 注意处理程序中嵌套的单引号。 That won't end well. 那不会很好地结束。 What's the browser supposed to do with this? 浏览器应该如何处理?

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

You really want to define a function and use that instead. 您确实想定义一个函数并使用 You avoid all of the pitfalls of passing a string to setTimeout() , multi-level quoting, etc. 您避免了将字符串传递给setTimeout() ,多级引用等的所有陷阱。

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

// ...

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

Rather than have some javascript inline in an onclick you should use .delay and .queue from jQuery. 而不是在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> 

I think pretty much all of these solutions would work. 我认为所有这些解决方案几乎都可以使用。 Here is another perhaps more elegant version of what you are doing up there, Chymmi. Chymmi,这是您在那里所做的另一个也许更优雅的版本。

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


Javascript 使用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 HTML

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

CSS 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