說我有50 div
,像這樣:是否可以通過點擊使用相同功能的不同元素來切換不同的div?
<div class="btn1"></div> //Toggles the first container to appear
<div class="btn2"></div> //Toggles the second container to appear
<div class="btn3"></div> //Toggles the third container to appear
,另有50 div
包含的信息,像這樣:
<div class="container-1"><h1>This is the first container</h1></div>
<div class="container-2"><h1>This is the second container</h1></div>
<div class="container-3"><h1>This is the third container</h1></div>
是否有可能做出相應div
撥動單擊各個按鈕時只有一個功能?我已經讀了一些關於代表團和在父母/兄弟姐妹身上運作的內容,但它似乎只能在打開同一個容器的多個按鈕上工作,而不是每個按鈕打開每個容器。
不知怎的,我不認爲寫一個函數,每格是要走的路。
'是有可能做出相應的div切換時,每個按鈕被點擊只有一個功能? - 是的。使用jQuery'on()'(或類似)綁定到'btn''' click''事件,然後可以使用解析來從'btn'中的類中獲取數字,以匹配'container'或添加一個數據屬性到每個div匹配他們像'data-id =「1」'或使用位置選擇器,如'兄弟姐妹'或類似如果'容器'div總是在'btn' div等相同的位置......選擇一個,試試&試試吧。如果您遇到問題,請發佈您的代碼,我們可以提供幫助。 – Nope
想要評論爲什麼在這裏提供的所有解決方案downvote? –
@KarthikGanesan我沒有。我刷新頁面,他們都downvoted - 不知道爲什麼,2用戶非常有幫助。 –