Is there a selector to exclude display: none elements?

前端 未结 6 2210
醉话见心
醉话见心 2021-02-19 23:22

I want to select only whose parents have display: block and exclude those whose parents have display

6条回答
  •  南笙
    南笙 (楼主)
    2021-02-19 23:53

    This is not possible with pure CSS so far, Unless you explicitly specify the inline css to style="display: none".

    You could use some javascript to filter a set of buttons that are visible.

    var buttons = document.querySelectorAll('.block button');
    
    var visibleButtons = [];
    
    buttons.forEach(function (element) {
      if (window.getComputedStyle(element.parentNode).display !== 'none') {
       visibleButtons.push(element);
      }
    });
    
    console.log(visibleButtons);
    .block {
      display: block;
    }
    
    .hidden {
      display: none;
    }

提交回复
热议问题