简体   繁体   English

如何仅获取父元素的可见子元素?

[英]How do I get only the visible children elements of a parent element?

I have tried:我努力了:

var $childNodes = $(".definition").children();
console.log($childNodes.filter(':visible').text());

from Get only visible text within an html element and child elements and I'm getting back all the elements from the parent element with a visibility set to hidden instead of visible .Get only visible text inside an html 元素和子元素,我从父元素中取回所有元素, visibility设置为hidden而不是visible

The full code is on repl preview panel完整代码在repl 预览面板

:visible works for display not for visibility , because visibility consumes space in layout, also opacity too. :visible用于显示而不是用于可见性,因为可见性会消耗布局中的空间,也不透明

 $(".definition").children().filter(function(){ return $(this).css("visibility")=="visible"});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="definition"> <div style= "visibility:hidden" class="definitionAgelenidae">Family:Agelenidae:Definition</div> <div style= "visibility:visible" class="definitionYorimaGenus">Genus:Yorima:Definition</div> <div style= "visibility:hidden" class="definitionYorima_angelica">Yorima angelica Roth:Definition</div> <div style= "visibility:hidden" class="definitionCybaeusGenus">Genus: Cybaeus:Definition</div> </div>

$(".definition").children().filter(function(index, ele){return $(ele).is(":visible")})

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

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