简体   繁体   中英

Jquery will not hide/show div

I'm trying to hide a Live Chat box on page load and then when a hyperlink is clicked, display the Chat box after 30 seconds.

Anybody have any ideas what I'm doing wrong?

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>

UPDATE The below works. I think the problem is with the chat script itself. It seems even when its inside the div it has a mind of its own.

<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>

You probably want to set timeout here and not delay . Like this,

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

so replace

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

Final Code

<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>

Change your existing display after 30 seconds ( $(thislive).delay(30000).show(); ) Code to

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

It will work

You need to do: $(thislive).delay(30000).show(0);

According to the documentation for .delay()

delay() can be used with the standard effects queue or with a custom queue. This will not delay the no-arguments forms of .show() or .hide() which do not use the effects queue.

When a duration is provided, .show() becomes an animation method.

Here's a fiddle

try using that code:

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

I resolved the issue by using the setTimeout() method. My chatBox hyperlink now has an onclick call to a new method called loadChat(). This then calls another method called showChat which then in turn loads the chat box script.

    <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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