显示元素的一部分但隐藏其余部分的好方法是什么? [英] What's a good way to show parts of an element but hide the rest?
问题描述
我想拥有一个javascript(jQuery)函数,该函数可以删除所有没有safe
类的东西.
I was wanting to have a javascript (jQuery) function that removed everything that didn't have the safe
class.
问题是,如果父元素被隐藏,则无法显示其安全"部分.
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.
trimmer = function(element){
x = $(element+' *:not(.safe)');
x.hide();
}
trimmer('section');
Fiddle
推荐答案
Horen是正确的,确实不可能显示隐藏元素的某些部分.
Horen was right, it is indeed impossible to show parts of a hidden element.
要仅使部分文本消失,必须将非safe
内容标记为要删除.
To make only parts of the text disappear, the non-safe
content must be labeled for removal.
$(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
这篇关于显示元素的一部分但隐藏其余部分的好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!