有些网页有一个“旋转”三角形控件,可以折叠子菜单。 我想要同样的行为,但是对于表单中的部分。
假设我有一张表格,其中包含贷款人、姓名、地址和城市信息。 我网站的一些用户将需要第二组这些字段。 我想为大多数用户隐藏额外的字段。 需要额外字段的人应该能够一键访问它们。 我该怎么做呢?
Some webpages have a "turning" triangle control that can collapse submenus. I want the same sort of behavior, but for sections in a form.
Say I had a form that had lender, name, address and city inputs. Some of my site's users are going to need a second set of these fields. I would like to conceal the extra fields for the majority of the users. The ones that need the extra fields should be able to access them with one click. How would I do that?
发布评论
评论(3)
啊,我想你的意思是你想在你的表单上有可折叠的部分。
简而言之:
这是更好的解释: 如何使用Javascript和CSS创建可折叠的DIV [更新2013-01-27该文章不再在网络上提供,您可以参考此 HTML 页面的源代码,获取受本文启发的应用示例]
或者如果您用 Google 搜索“CSS 折叠部分”之类的词,您会发现许多其他教程,包括超级花哨的教程(例如 http://xtractpro.com/articles/Animated-Collapsible-Panel.aspx)。
Ah, I think you mean you want to have collapsible sections on your form.
In short:
Here's a better explanation: How to Create a Collapsible DIV with Javascript and CSS [Update 2013-01-27 the article is no longer available on the Web, you can refer to the source of this HTML page for an applied example inspired by this article]
Or if you Google search with words like "CSS collapsing sections" or such you will find many other tutorials, including super-fancy ones (e.g. http://xtractpro.com/articles/Animated-Collapsible-Panel.aspx).
使用 JavaScript 隐藏/显示元素的最基本方法是设置元素的可见性属性。
鉴于您的示例,假设您在页面上定义了以下表单:
这里有两件事需要注意:
showElement()
采用元素 id 作为参数,如下所示:几乎所有 JavaScript 方法都会在幕后执行此操作,但我建议使用框架这对您隐藏了实现细节。 查看 JQuery 和 JQuery UI 以便在隐藏/显示元素时获得更加完美的过渡。
Your absolute most basic way of hiding/showing an element using JavaScript is by setting the visibility property of an element.
Given your example imagine you had the following form defined on your page:
There are two things to note here:
showElement()
takes an element id as a parameter and looks like this:Almost every JavaScript approach is going to be doing this under the hood, but I would recommend using a framework that hides the implementation details away from you. Take a look at JQuery, and JQuery UI in order to get a much more polished transition when hiding/showing your elements.
这是一个使用复选框的简单纯 CSS 解决方案:
Here's a simple css only solution that uses a checkbox: