【问题标题】:Hide woocommerce-message in couple seconds在几秒钟内隐藏 woocommerce 消息
【发布时间】:2016-06-25 18:46:48
【问题描述】:

我尝试使用此代码隐藏 woocommerce-message,但它不起作用。

  setTimeout(function() {
    $('.woocommerce-message').fadeOut('fast');
}, 5000); // <-- time in mseconds

您还有其他解决方案吗?我尝试将此代码放入 header.php 并且我的模板有自定义标头 JavaScript 代码的空间,但没有任何效果。

【问题讨论】:

  • 如果元素在开始时不存在,并且随机出现,我假设您必须检查此消息何时出现,然后调用设置超时

标签: javascript php html wordpress woocommerce


【解决方案1】:

如果元素弹出并随机插入到 dom 中,您可以使用这样的方法将其删除。

$(document).ready(function(){
  
   $(document).on('DOMNodeInserted', function(e) {
        if ($(e.target).is('.woocommerce-message')) {
           
           setTimeout(function() {
             $('.woocommerce-message').fadeOut('fast');
            alert("node fading"); 
           }, 5000);
        }
    });

   //object inserted after 2 seconds
   setTimeout(function(){
       $('<div class="woocommerce-message">Test node inserted </div>').appendTo('body');
        alert("node inserted");
     },2000);
});
   
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

【讨论】:

    【解决方案2】:

    这对我有用。

        setTimeout(function() {
            $('.woocommerce-message').fadeOut('fast') 
        }, 5000);
    

    如果您使用的是 wordpress,请尝试

        setTimeout(function() {
            jQuery('.woocommerce-message').fadeOut('fast') 
        }, 5000);
    

    此链接也可能对您有所帮助:jQuery | on click fade out and / or automatically fade out

    【讨论】:

    • 太棒了。非常感谢。这个适用于 wordpress 的效果很好。
    猜你喜欢
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多