简体   繁体   English

jQuery默认情况下不隐藏元素

[英]jQuery not hiding elements by default

Hi I'm using quite a few jQuery elements in a web application of mine. 嗨,我在我的Web应用程序中使用了很多jQuery元素。 I am using jQuery UI Tabs and I am trying to put in some show hide elements within this tab, however if I put in some custom jQuery this tab fails and all other jQuery on the page fails to do what I want on default. 我正在使用jQuery UI选项卡,我试图在此选项卡中放入一些show hide元素,但是如果我放入一些自定义jQuery,则此选项卡失败并且页面上的所有其他jQuery都无法执行我想要的默认设置。

For example i have one div hidden by default <div class="partnersiteinfo"> however this is shown on default instead and have to click to toggle hide. 例如,我默认隐藏了一个div <div class="partnersiteinfo">但是这默认显示,而且必须单击才能切换隐藏。

Not sure what is going wrong here, not got a lot of experience with jquery, so would appreciate some help, personally I think the error is in the JS, so I've made a jsFiddle or view my js below: 不知道这里出了什么问题,没有很多jquery的经验,所以会感谢一些帮助,我个人认为错误是在JS中,所以我做了一个jsFiddle或查看我的js如下:

js/js.js JS / js.js

$(document).ready(function () {
    $('.partnersiteinfo').hide();
    $('.toggle').on('click', function () {
        $('.partnersiteinfo').slideToggle();

        return false;
    });
    $(function () {
        $('#sortsitemap').on('click', '.toggle', function () {
            //Gets all <tr>'s  of greater depth
            //below element in the table
            var findChildren = function (tr) {
                var depth = tr.data('depth');
                return tr.nextUntil($('tr').filter(function () {
                    return $(this).data('depth') <= depth;
                }));
            };
            var el = $(this);
            var tr = el.closest('tr'); //Get <tr> parent of toggle button
            var children = findChildren(tr);

            //Remove already collapsed nodes from children so that we don't
            //make them visible. 
            //(Confused? Remove this code and close Item 2, close Item 1 
            //then open Item 1 again, then you will understand)
            var subnodes = children.filter('.expand');
            subnodes.each(function () {
                var subnode = $(this);
                var subnodeChildren = findChildren(subnode);
                children = children.not(subnodeChildren);
            });

            //Change icon and hide/show children
            if (tr.hasClass('collapse')) {
                tr.removeClass('collapse').addClass('expand');
                children.hide();
            } else {
                tr.removeClass('expand').addClass('collapse');
                children.show();
            }
            return children;
        }).find('.toggle').trigger('click');

    });
    $(function () {
        $("#tabs").tabs();
    });
});

Don't hide elements with JavaScript. 不要使用JavaScript隐藏元素。 Use CSS instead. 请改用CSS。 Also you do trigger the click on the .toggle which shows the .partnersiteinfo , this is why it becomes visible by default. 此外,您还会触发显示.partnersiteinfo.toggle上的click ,这就是默认情况下它变为可见的原因。

You are triggering the click event in your fiddle and that's the reason its visible. 你正在触发小提琴中的点击事件,这就是它可见的原因。 Remove this part and it will work. 删除此部分,它将工作。

find('.toggle').trigger('click');

Your Fiddle Updated. 你的小提琴更新了。

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

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