简体   繁体   English

如何更改jquery .toggle()以使用display:table-cell?

[英]How to change jquery .toggle() to use display:table-cell?

I am using jquery .toggle() to show a div on a page that has display:none on page load. 我正在使用jquery .toggle()在页面加载上显示display:none的页面上显示div。 However, under the default settings jquery inserts display:block, where I would want display:table-cell. 但是,在默认设置下,jquery插入display:block,我想要显示:table-cell。 How can I achieve this? 我怎样才能做到这一点? My attempt so far: 我到目前为止的尝试:

<div class="mydiv" style"display:none">test</div>

.mydiv {
display:table-cell;
}

$("a#showdiv").click(function() {
    $(".mydiv").toggle();

Use .toggleClass() instead and use css for the styling.. 使用.toggleClass()代替并使用css进行样式化。

html HTML

<div class="mydiv table-hidden">test</div>

css CSS

.mydiv {
    display:table-cell;
}
.mydiv.table-hidden{
    display:none;
}

jquery jQuery的

$("a#showdiv").click(function() {
    $(".mydiv").toggleClass('table-hidden');
}

Use CSS for the styling @Gaby aka G. Petrioli or use the .css() method in jQuery. 使用CSS作为样式@Gaby又名G. Petrioli或在jQuery中使用.css()方法。

HTML HTML

<div class="mydiv">test</div>

jQuery jQuery的

$("a#showdiv").click(function() {
    if($(".mydiv").css("display") == "none"){
      $(".mydiv").css("display", "table-cell");
    } else {
      $(".mydiv").css("display", "none");
    }
});

What you have should work already. 你有什么应该工作。

In jQuery, unless the style attribute of the element has display initially set to something else other than none , when the show is called, all it does is remove the style attribute for display. 在jQuery的,除非该元素的样式属性已display初始设置为别的东西比其他none ,当show被调用时,它的作用是除去用于显示的样式属性。 It doesn't set it block. 它没有设置阻止。

You can see for yourself in this fiddle that when the button is clicked, the display that is set in the CSS is what the element is set to. 你可以在这个小提琴中看到,单击按钮时,CSS中设置的display就是元素的设置。

Here's the relevant code in the jQuery source : 这是jQuery源代码中的相关代码:

function showHide( elements, show ) {
    var display, elem, hidden,
        values = [],
        index = 0,
        length = elements.length;

    for ( ; index < length; index++ ) {
        elem = elements[ index ];
        if ( !elem.style ) {
            continue;
        }

        values[ index ] = data_priv.get( elem, "olddisplay" );
        display = elem.style.display;
        if ( show ) {
            // Reset the inline display of this element to learn if it is
            // being hidden by cascaded rules or not
            if ( !values[ index ] && display === "none" ) {
                elem.style.display = "";
            }

            // Set elements which have been overridden with display: none
            // in a stylesheet to whatever the default browser style is
            // for such an element
            if ( elem.style.display === "" && isHidden( elem ) ) {
                values[ index ] = data_priv.access( elem, "olddisplay", css_defaultDisplay(elem.nodeName) );
            }
        } else {

            if ( !values[ index ] ) {
                hidden = isHidden( elem );

                if ( display && display !== "none" || !hidden ) {
                    data_priv.set( elem, "olddisplay", hidden ? display : jQuery.css(elem, "display") );
                }
            }
        }
    }

    // Set the display of most of the elements in a second loop
    // to avoid the constant reflow
    for ( index = 0; index < length; index++ ) {
        elem = elements[ index ];
        if ( !elem.style ) {
            continue;
        }
        if ( !show || elem.style.display === "none" || elem.style.display === "" ) {
            elem.style.display = show ? values[ index ] || "" : "none";
        }
    }

    return elements;
}

I should note that toggling classes will usually be faster in general because there is less information to check. 我应该注意,切换类通常会更快,因为要检查的信息较少。

.display-none {
    display: none;
}
div.display-table-cell {
    display: table-cell;
}

<button id="showdiv">Show/Hide</button>
<div class="mydiv display-none">test</div>
<div class="mydiv display-none">test</div>
<div class="mydiv display-none">test</div>
<div class="mydiv display-none">test</div>

$("#showdiv").click(function() {
    $(".mydiv").toggleClass('display-table-cell');
});

http://jsfiddle.net/7q27y/ http://jsfiddle.net/7q27y/

If you understand CSS precedence as well, you don't technically "need" the div. 如果你也理解CSS优先级,那么你在技术上并不“需要” div. on the latter: 关于后者:

div.display-table-cell {
    display: table-cell;
}

http://jsfiddle.net/7q27y/ http://jsfiddle.net/7q27y/

Of course, this is due to it's precedence and the fact it falls after the other statement, which both would calculate as the same precedence otherwise. 当然,这是由于它的优先级以及它落在另一个语句之后的事实,否则两者都将计算为相同的优先级。 See: 看到:

http://jsfiddle.net/7q27y/2/ http://jsfiddle.net/7q27y/2/

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

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