简体   繁体   English

显示元素的一部分但隐藏其余部分的好方法是什么?

[英]What's a good way to show parts of an element but hide the rest?

I was wanting to have a javascript (jQuery) function that removed everything that didn't have the safe class. 我想拥有一个javascript(jQuery)函数,该函数可以删除所有没有safe类的内容。

The problem is, if the parent element is hidden, it cannot show the 'safe' part of it. 问题是,如果父元素被隐藏,则无法显示其“安全”部分。

Is there a simple way to get around this? 有没有简单的方法可以解决此问题? I'd rather not go in and span all of the elements that need removed. 我宁愿不要span所有需要删除的元素。

trimmer = function(element){
    x = $(element+' *:not(.safe)');
    x.hide();   
}
trimmer('section');

Fiddle 小提琴

var element = 'section';

//finds all non `.safe` elements in `section`s and hides them
$(':not(.safe)', element).hide();  

//finds all `.safe` elements in `section`s and shows the `section`s
$('.safe', element).parents(element).show();  

Horen was right, it is indeed impossible to show parts of a hidden element. 霍伦是对的,确实不可能显示出隐藏元素的某些部分。

To make only parts of the text disappear, the non- safe content must be labeled for removal. 为了仅使文本的一部分消失,必须标记safe内容以将其删除。

$(element).contents().each(function() {
  if (this.nodeType == 3)
    $(this).wrap('<span class="disappear" />');
});

You can read more about this answer here: How to add spans to all areas of a node that isn't restricted 您可以在此处阅读有关此答案的更多信息: 如何将跨度添加到不受限制的节点的所有区域

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

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