我有一個頁面有多個div,我想彼此獨立打開和關閉。到目前爲止我的代碼是在這裏:http://jsfiddle.net/Einulfr/q4ra0gbb/JQuery - 多個div獨立打開/關閉
的Javascript:
$(document).ready(function() {
$("#hide").click(function() {
$("#show").show();
$(".expandedText").hide();
});
$("#show").click(function() {
$("#show").hide();
$(".expandedText").show();
});
});
HTML:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h2>Title of First Article</h2>
<p>Story introduction paragraph here...</p>
<a href="#" id="show">Read More...</a>
<div class="expandedText" style="display: none;>
<p>Story continued here in second paragraph...</p>
<p>Story continued here in third paragraph...</p> <a href="#" id="hide">Close Article</a>
</div>
<h2>Title of Second Article</h2>
<p>Story introduction paragraph here...</p>
<a href="#" id="show">Read More...</a>
<div class="expandedText" style="display: none;>
<p>Story continued here in second paragraph...</p>
<p>Story continued here in third paragraph...</p> <a href="#" id="hide">Close Article</a>
</div>
但正如你所看到的,而第一個工作正常,後續的div不和也開放和關閉第一個div的操作。 我也希望'Read More ...'在點擊時消失,當我點擊'Close Article'時返回...目前這種方法可行,但對類似問題的其他解決方案似乎只是在最初的閱讀更多...)鏈接,並要求重新點擊原始鏈接以再次隱藏它。我希望這是有道理的:/
我是一個完全新手,當談到這種事情;我錯過了什麼/做錯了什麼?
非常感謝提前
ID是單數。 – epascarello
就像@epascarello提到的一樣,一個元素ID應該是唯一的頁面。 – Sid