[英]Little folding triangles: how can I create collapsible sections on a webpage?
某些網頁具有可折疊子菜單的“轉向”三角形控件。 我想要相同的行為,但要針對表單中的部分。
假設我的表格有放款人,姓名,地址和城市信息。 我網站的某些用戶將需要第二組這些字段。 我想為大多數用戶隱藏額外的字段。 那些需要額外字段的用戶應該能夠一鍵訪問它們。 我該怎么做?
嗯,我想您的意思是您想在表單上包含可折疊的部分。
簡而言之:
這是一個更好的解釋: 如何使用Javascript和CSS創建可折疊的DIV [ 更新2013-01-27,該文章不再在Web上提供,您可以參考此HTML頁面的源代碼以獲取受本文啟發的應用示例。 ]
或者,如果您使用諸如“ CSS折疊部分”之類的詞進行Google搜索,您會發現許多其他教程,包括超級教程(例如http://xtractpro.com/articles/Animated-Collapsible-Panel.aspx )。
使用JavaScript隱藏/顯示元素的最基本方法就是設置元素的visible屬性。
以您的示例為例,假設您在頁面上定義了以下表單:
<form id="form1">
<fieldset id="lenderInfo">
<legend>Primary Lender</legend>
<label for="lenderName">Name</label>
<input id="lenderName" type="text" />
<br />
<label for="lenderAddress">Address</label>
<input id="lenderAddress" type="text" />
</fieldset>
<a href="#" onclick="showElement('secondaryLenderInfo');">Add Lender</a>
<fieldset id="secondaryLenderInfo" style="visibility:hidden;">
<legend>Secondary Lender</legend>
<label for="secondaryLenderName">Name</label>
<input id="secondaryLenderName" type="text" />
<br />
<label for="secondaryLenderAddress">Address</label>
<input id="secondaryLenderAddress" type="text" />
</fieldset>
</form>
這里有兩件事要注意:
showElement()
將元素id作為參數,如下所示:
function showElement(strElem) {
var oElem = document.getElementById(strElem);
oElem.style.visibility = "visible";
return false;
}
幾乎每種JavaScript方法都會在后台進行此操作,但是我建議您使用一個框架,將實現細節隱藏起來。 看一下JQuery和JQuery UI ,以便在隱藏/顯示元素時獲得更加優美的過渡。
這是使用復選框的簡單的css only解決方案:
.hideNext:not(:checked)+div { max-height: 0; overflow: hidden; }
<label for="ch1">More Options</label><input id="ch1" type="checkbox" class="hideNext" /> <div>Whatever you want to hide.</div> and then more stuff below..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.