我是撰寫 Jquery/Javascript 腳本的新手,實際上我有些掙扎。如何在不影響其他專案的情況下顯示同一類的一個專案?
我的代碼:
$(function() {
$('.embedContainer > .myPosterImage').on('click', function() {
$('.embedContainer > .myPosterImage').hide();
$('.embedContainer > embed').show();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="embedContainer">
<img class="myPosterImage" src="img/websites/01_documentation.jpg" data-original="img/websites/full/01_full.jpg" />
<embed src="img/websites/Concept.pdf" width="563.41" height="375.98"></embed>
</div>
<div class="embedContainer">
<img class="myPosterImage" src="img/websites/01_documentation.jpg" data-original="img/websites/full/01_full.jpg" />
<embed src="img/websites/Concept.pdf" width="563.41" height="375.98"></embed>
</div>
非常感謝。
uj5u.com熱心網友回復:
第一行將隱藏所有這些,這可能是您想要做的(例如,如果您想折疊所有并展開單擊的那個):
$('.embedContainer > .myPosterImage').hide();
但正如您所發現的,第二行也將顯示所有這些:
$('.embedContainer > embed').show();
您可以做的是從被單擊的元素(this在單擊事件處理程式中)開始遍歷 DOM 。在這種情況下,您將向上遍歷 DOM(closest()例如使用)到一個公共父元素,然后回傳(find()例如使用)到目標元素。
像這樣的東西:
$(this).closest('.embedContainer').find('embed').show();
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/390265.html
標籤:javascript html 查询 css 脚本编写
上一篇:phpobstart(),$output2=ob_get_clean();
下一篇:JS-陣列中的日期,鏈接構建
