显示元素的一部分但隐藏其余部分的好方法是什么? [英] What's a good way to show parts of an element but hide the rest?

查看:141
本文介绍了显示元素的一部分但隐藏其余部分的好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆