繁体   English   中英

jQuery将不会隐藏/显示div

[英]Jquery will not hide/show div

我试图在页面加载时隐藏“实时聊天”框,然后单击超链接时,在30秒后显示“聊天”框。

有人知道我在做什么错吗?

jQuery的

<script type="text/javascript">
    jQuery(document).ready(function ($) {
        var thislive = document.getElementById('liveChat');
        $(thislive).hide();

        $("#chatBox").click(function () {
            var thislive = document.getElementById('liveChat');
            $(thislive).delay(30000).show(); // Display after 30 seconds
        });
    });
</script>

<div id="liveChat">
    <!-- Start of LiveChat (www.livechatinc.com) code -->
    <script type="text/javascript">
        window.__lc = window.__lc || {};
        window.__lc.license = 111111;
        (function () {
            var lc = document.createElement('script'); lc.type = 'text/javascript'; lc.async = true;
            lc.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'cdn.livechatinc.com/tracking.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(lc, s);
        })();
    </script>
    <!-- End of LiveChat code -->
</div>

cshtml

            <div class="col-sm-3 col-md-3">
                <a id="chatBox" href="#panel-column-3" class="panel-column row-collapse-toggle collapsed" data-toggle="collapse" aria-expanded="false" aria-controls="bpanel-column-3">
                    <i class="fa fa-question-circle"></i>
                    <h3>@Umbraco.Field("contactustab3title")</h3>
                </a>
                <div class="row-collapse collapse" id="panel-column-3">
                    <div class="inner">
                        @Umbraco.Field("contactustab3content")
                    </div>
                </div>
            </div>

更新以下工作。 我认为问题出在聊天脚本本身。 看起来,即使它在div中也有自己的想法。

<script type="text/javascript">
    jQuery(document).ready(function ($) {
    var thislive = document.getElementById('liveChat');
    $(thislive).hide();

        $("#chatBox").click(function () {
            var thislive = document.getElementById('liveChat');
            $(thislive).delay(5000).show(0); //5 seconds
        });
    });
</script>

<div id="liveChat">
    <div>
        test
    </div>
</div>

您可能想在此处设置timeout而不是delay 像这样,

 setTimeout(function(){ $(thislive).show(); }, 30000);

所以更换

$(thislive).delay(30000).show();

最终密码

<script type="text/javascript">
    jQuery(document).ready(function ($) {
        var thislive = document.getElementById('liveChat');
        $(thislive).hide();

        $("#chatBox").click(function () {
            var thislive = document.getElementById('liveChat');
             setTimeout(function(){ $(thislive).show(); }, 30000); // Display after 30 seconds
        });
    });
</script>

30秒后更改现有显示( $(thislive).delay(30000).show(); )代码为

$(thislive).delay(30000).show(0);

会工作的

您需要执行以下操作: $(thislive).delay(30000).show(0);

根据.delay()的文档

delay()可以与标准效果队列或自定义队列一起使用。 这不会耽误的无参数形式.show().hide()不使用效果队列。

提供持续时间后, .show()会成为动画方法。

这是一个小提琴

尝试使用该代码:

    $(document).ready(function($){
$('#liveChat').hide();
$('#chatBox').click(function(){
$('#liveChat').delay(30000).fadeIn();
});
});

我通过使用setTimeout()方法解决了该问题。 我的chatBox超链接现在具有对名为loadChat()的新方法的onclick调用。 然后,这将调用另一个名为showChat的方法,该方法随后将加载聊天框脚本。

    <script type="text/javascript">
    function loadChat() {
        setTimeout(showChat, 5000) // load the chat icon after 5 seconds
    }

    function showChat() {
        window.__lc = window.__lc || {};
        window.__lc.license = 1111111;
        (function () {
            var lc = document.createElement('script'); lc.type = 'text/javascript'; lc.async = true;
            lc.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'cdn.livechatinc.com/tracking.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(lc, s);
        })();
    }
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM