如何使用 . hide () 、.show () 或.toggle () 来切换元素的可见度 ?

如果元素可见或隐藏, 我如何测试 ?


当前回答

您可以使用此功能:

$(element).is(':visible');

示例代码

$( document) {( pready) {( ) {( ) {( ) $( ” tggle” ) . click () {( ) {( ) {( ) {( ) {( ) {( ) $( ) ) {( ) $( ) ) } . 点击 ( ) {( ) {( ) {( ” ) } } } } ; & ; 标注 src=" https://ajaax.googleapis.com/ax/libs/ jquery/ 12.2/ query. min. js> & () / statict> & pl= "content" & gt; this is a

其他回答

而不是为每个元素写入事件, 请这样做 :

$('div').each(function(){
  if($(this).css('display') === 'none'){
    $(this).css({'display':'block'});
  }
});

您也可以在以下输入中使用:

$('input').each(function(){
  if($(this).attr('type') === 'hidden'){
    $(this).attr('type', 'text');
  }
});

您可以使用此功能:

$(element).is(':visible');

示例代码

$( document) {( pready) {( ) {( ) {( ) $( ” tggle” ) . click () {( ) {( ) {( ) {( ) {( ) {( ) $( ) ) {( ) $( ) ) } . 点击 ( ) {( ) {( ) {( ” ) } } } } ; & ; 标注 src=" https://ajaax.googleapis.com/ax/libs/ jquery/ 12.2/ query. min. js> & () / statict> & pl= "content" & gt; this is a

content.style.display != 'none'

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\可以\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

用于检查元素是否可见、不显示,甚至不透明度水平的扩展功能

如果元素不可见, 它返回错误 。

function checkVisible(e) {
    if (!(e instanceof Element)) throw Error('not an Element');
    const elementStyle = getComputedStyle(e);
    if (elementStyle.display === 'none' || elementStyle.visibility !== 'visible' || elementStyle.opacity < 0.1) return false;
    if (e.offsetWidth + e.offsetHeight + e.getBoundingClientRect().height +
        e.getBoundingClientRect().width === 0) {
        return false;
    }
    const elemCenter   = {
        x: e.getBoundingClientRect().left + e.offsetWidth / 2,
        y: e.getBoundingClientRect().top + e.offsetHeight / 2
    };
    if (elemCenter.x < 0 || elemCenter.y < 0) return false;
    if (elemCenter.x > (document.documentElement.clientWidth || window.innerWidth)) return false;
    if (elemCenter.y > (document.documentElement.clientHeight || window.innerHeight)) return false;
    let pointContainer = document.elementFromPoint(elemCenter.x, elemCenter.y);
    do {
        if (pointContainer === e) return true;
    } while (pointContainer = pointContainer.parentNode);
    return false;
}

如果您想要检查页面上是否可见元素, 取决于其父的可见度, 您可以检查元素的宽度和高度是否均等于 0 。

jquery 圆

$ementle. width () 0

香草香草

元素. clishight = 0 元素. clishight 0

e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e. e.