与普通JavaScript中的.show()-jQuery等效

与普通JavaScript中的.show()-jQuery等效,javascript,jquery,css,Javascript,Jquery,Css,有人知道javascript中jquery的.show()等价物是什么吗 我曾尝试使用document.getElementById和添加/删除我命名为“show”/“hide”的类,但效果不太好,我不确定这些类中可能有错误的属性 表演班是: .show{ 位置:相对位置; 显示:块; } 隐藏类是: .hide{ 位置:绝对位置; 顶部:0px; 右:0px; 显示:无; } 我很肯定有更好的办法 使用document.getElementById(“MyId”).className='s

有人知道javascript中jquery的.show()等价物是什么吗

我曾尝试使用document.getElementById和添加/删除我命名为“show”/“hide”的类,但效果不太好,我不确定这些类中可能有错误的属性

表演班是:

.show{
位置:相对位置;
显示:块;
}
隐藏类是:

.hide{
位置:绝对位置;
顶部:0px;
右:0px;
显示:无;
}

我很肯定有更好的办法

使用
document.getElementById(“MyId”).className='show'
document.getElementById('myElement').style.display = 'block'; // show
document.getElementById('myElement').style.display = 'none'; // hide

document.getElementById(“MyId”).className='hide'
尝试使用document.getElementsByClassName,因为您正在使用.show和.hide。这些是类,您正试图按Id设置目标。免责声明:您可能需要仔细检查旧浏览器对此的支持。

jQuery在隐藏之前考虑了
display
的值。当您触发
show()
时,它会将其返回到该值。因此,它不仅仅是简单地将元素的
display
属性设置为
block
none

所以基本上:

function hide(){
    //get previous display value
    //store it in an internal cache. jQuery has an internal data storage
    //hide element
}

function show(){
    //get previous display value for that element
    //apply to element to show it
}

匹配的元素将立即显示,没有动画。这大致相当于调用
.css('display','block')
,只是display属性恢复为最初的状态。如果元素的显示值为
内联
,则隐藏并显示后,它将再次内联显示


在jQuery之前,我使用以下方法显示/隐藏元素:

HTMLElement.prototype.toggleDisplay = function(on_or_off, display_type) {
    if (typeof(tddisptype) == "undefined") tddisptype = "block";
    var new_display;
    if (typeof(on_or_off) != "boolean") {
        if (this.style.display == "none") new_display = display_type;
        else new_display = "none";
    } else {
        if (on_or_off) new_display = display_type;
        else new_display = "none";
    }
    this.style.display = new_display;
}​

这将向所有元素添加一个toggleDisplay()函数;e、 例如,可以通过document.getElementById()获取的。您可以将它作为参数传递给
true
false
,以显示或隐藏元素,或者如果您不传递参数,它将尝试确定是显示还是隐藏元素。第二个参数指定与“打开”状态关联的显示类型;上面,它默认为
block

是的,非常感谢这正是我要找的。@freshyeball不,它完全不一样。并非所有元素都是
display:block。有内联、内联块、表、表单元格、列表项等等。jQuery show不会使这些
显示:block
。是的,jQuery会考虑您正在显示的内容。。。span应该是style.display='inline'。。。等这不是一个好主意equivalent@Esailija请注意,如果要使用具有
display:none
的类,则不需要该类中的其他属性。