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

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


当前回答

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

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

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

其他回答

content.style.display != 'none'

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

只需检查该元素是否可见, 并返回一个布尔。 jquery 通过在元素中添加无显示来隐藏元素, 所以如果您想要使用纯正的 javascript, 您仍然可以这样做, 例如 :

if (document.getElementById("element").style.display === 'block') {
  // Your element is visible; do whatever you'd like
}

您也可以使用jquery, 因为您的代码的其余部分似乎使用这个代码, 而您有小块代码。 类似在jquery的下面的东西, 也为您玩同样的把戏:

if ($(element).is(":visible")) {
    // Your element is visible, do whatever you'd like
};

使用 cs 方法在 jquery 中也可以产生同样的结果 :

if ($(element).css('display') === 'block') {
    // Your element is visible, do whatever you'd like
}

在检查能见度和显示情况时,您还可以做以下工作:

if ($(this).css("display") === "block" || $(this).css("visibility") === "visible") {
   // Your element is visible, do whatever you'd like
}

您可以使用此功能:

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

示例代码

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

   hideShow(){
  $("#accordionZiarat").hide();
  // Checks CSS content for display:[none|block], ignores visibility:[true|false]
  if ($("#accordionZiarat").is(":visible")) {
    $("#accordionZiarat").hide();
  }

  
  else if ($("#accordionZiarat").is(":hidden")) {
    $("#accordionZiarat").show();
  }

  else{

  }

使用任何可见选择器或隐藏选择器检查粘度:

: 可见选择器 - jquery (“ : 可见”) 使用 : 隐藏选择器 - jquery (“ : 隐藏” )

使用.toggle () - 显示和隐藏元素

{{{{{{{{{{{{{{{}检查元素是否隐藏,并返回真实的虚假控制台.log($(“元素”)){{{{{{}}}检查元素是否可见,并返回真实的虚假控制台.log($(“元素”){{}}}{($(“元素”){}}}如果($(“元素”){}}}{{}}{{{}{{}}{{{{{}}可识别性{}}}}}($(“glegle}}}}}lock(funct}}}。