如何隐藏所有小于特定像素的文本并稍后使用 Javascript 将它们恢复?
我正在开发一个项目,在某些情况下需要隐藏所有小文本(例如小于 12px),并在其他一些事件中将它们带回来。它不是一个网站,而是 webkit 浏览器上发生的事情。我无法控制页面上的内容(由第三方开发人员开发),但可以控制修改它。我知道我可以循环遍历所有标签元素并检查字体大小,如果小于 12px 则隐藏它们,但这不仅效率低下,而且可以更改文本以再次显示,例如在 ajax 调用之后,这是“禁止的”。其他解决方案是每隔几秒运行一次循环,但这是一个昂贵的过程。
其他任务是在其他事件上显示小文本,仅使用简单的自定义类即可实现这一点并不太困难。
I'm working on a project, which in some cases requires to hide all small text (eg. less than 12px), and on some other event, bring them back. It's not a website, but something happening on the webkit browser. I don't have control over the content on the page (developed by the third party developers), but have control to modify it. I know I can loop through all tag elements and check font sizes and hide them if smaller than 12px, but it's not only inefficient, but the text can be changed to be shown again, say after an ajax call, which is "prohibited". Other solution would be to run that loop every couple seconds, but it's an expensive process.
Other task is to show small text on some other event, which is not too difficult to implement by just using simple custom class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在页面加载时运行代码,然后在任何 AJAX 调用完成时使用 jQuery 的全局 AJAX 事件处理程序: http://api.jquery.com/category/ajax/global-ajax-event-handlers/
你可以通过
findSmallText
函数有两个参数:$root
:(jQuery 对象)开始寻找小文本的根元素,尽可能限制这个以提高性能(所以不必要的元素不要不必扫描)。state
:(字符串)display
属性添加到带有小文本的元素上,可以使用block
来显示元素。You can run the code on page-load, and then when any AJAX call completes using jQuery's Global AJAX Event Handlers: http://api.jquery.com/category/ajax/global-ajax-event-handlers/
You can pass the
findSmallText
function two arguments:$root
: (jQuery object) the root element to start looking for small text, limit this as much as possible to increase performance (so unnecessary elements don't have to be scanned).state
: (string) thedisplay
property to add to the elements with small text, you can useblock
to show the elements.如果 HTML 结构没有改变(没有通过 AJAX 添加额外的容器),只需分析 onLoad 页面(有点像 Jasper 建议的那样),而不是在每次 AJAX 调用后重新运行分析,而是添加一个新类 - 让我们称之为
.HideMeInCertainCases
为了好玩。这样,您就可以随时使用简单的选择器隐藏/显示您想要的所有内容。因此,不要使用
$(this).css('display', state);
使用$(this).addClass('HideMeInCertainCases');
当事件发生时您正在谈论的情况发生后,您可以使用此选择器
$("HideMeInCertainCases").toggleClass("hideMe")
切换显示状态。直接更改显示属性可能会破坏布局,因为包含文本的节点可能有不同的显示(块、内联、内联块...)。当然.hideMe { display:none; }
应该位于样式表中的某个位置。如果您希望布局保持不变并且仅隐藏内容,请使用visibility
而不是display
if the HTML structure doesnt change (no extra containers added thru AJAX) simply analyze the page onLoad (kinda like what Jasper suggests) but instead of re-running the analysis after each AJAX call you add a new class - let's call it
.HideMeInCertainCases
for the fun of it. That way you can hide / show everything you want with a simple selector whenever you want.So instead of this line:
$(this).css('display', state);
use$(this).addClass('HideMeInCertainCases');
When the event you were talking about occurs you can then toggle the display state with this selector
$("HideMeInCertainCases").toggleClass("hideMe")
. Changing the display-attribute directly might break your layout as the nodes containing text might have different displays to begin with (block, inline, inline-block...). Of course.hideMe { display:none; }
should be somewhere in your stylesheet. If you want the layout to stay the same and only hide the content usevisibility
instead ofdisplay