其他回答你的問題完美無缺,但我只是想我會另闢蹊徑。儘可能將HTML標記,CSS樣式和JavaScript代碼分開是一個好主意。考慮到這一點,最簡單的隱藏方式是使用一個類。它允許在其所屬的CSS中定義「隱藏」。使用這種方法,您稍後可以決定通過使用CSS transition
向上滾動或逐漸消失來隱藏ul
,而不用更改HTML或代碼。這個時間更長,但我覺得這是一個更好的整體解決方案。
演示:http://jsfiddle.net/ThinkingStiff/RkQCF/
HTML:
<a id="showTags" href="#" title="Show Tags">Show All Tags</a>
<ul id="subforms" class="subforums hide"><li>one</li><li>two</li><li>three</li></ul>
CSS:
#subforms {
overflow-x: visible;
overflow-y: visible;
}
.hide {
display: none;
}
腳本:
document.getElementById('showTags').addEventListener('click', function() {
document.getElementById('subforms').toggleClass('hide');
}, false);
Element.prototype.toggleClass = function (className) {
if(this.className.split(' ').indexOf(className) == -1) {
this.className = (this.className + ' ' + className).trim();
} else {
this.className = this.className.replace(new RegExp('(\\s|^)' + className + '(\\s|$)'), ' ').trim();
};
};
你使用jQuery? – jQuerybeast