简体   繁体   English

jQuery:单击更改具有全局代码的相似元素

[英]jQuery: on click change similar elements with global code

I have a simple code where when I click on one of the 'chevron' jquery show hided content.我有一个简单的代码,当我单击“雪佛龙”jquery 之一时显示隐藏的内容。

But this code is completly for each one different.但是这段代码对于每一个都是完全不同的。

Is there any solution how to make one global code to effect every element with this functionality?是否有任何解决方案如何制作一个全局代码来影响具有此功能的每个元素?

In my original code, I have more elements like this.在我的原始代码中,我有更多这样的元素。

My code:我的代码:

 $('.chevron1').click(function() { $('.more-fix-broken-links').toggleClass('active'); }); $('.chevron2').click(function() { $('.more-spelling-grammar').toggleClass('active'); });
 img { width: 10px }.chevron { cursor: pointer; }.chevron.active { transform: rotate(90deg); }.column { display: flex; flex-direction: column; }.more-fix-broken-links { max-width: 280px; display: none; }.more-spelling-grammar { max-width: 280px; display: none; }.more-fix-broken-links.active { display: block; height: 100%; }.more-spelling-grammar.active { display: block; height: 100%; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="column"> <div> <input id="fix-broken-links" type="checkbox"> <label for="fix-broken-links">Fix broken links</label> <img class="chevron1" src="https://supersede.space/chevron.svg" alt="Checklist chevron"> </div> <div> <p class="more-fix-broken-links">SKuska skuskaSKuska skuskaSKuska skuskaSKuska skuskaSKuska skuska</p> </div> </div> <div class="column"> <div> <input id="spelling-grammar" type="checkbox"> <label for="spelling-grammar">Spelling & grammar</label> <img class="chevron2" src="https://supersede.space/chevron.svg" alt="Checklist chevron"> </div> <div> <p class="more-spelling-grammar">SKuska skuskaSKuska skuskaSKuska skuskaSKuska skuskaSKuska skuska</p> </div> </div>

You can store each class and the corresponding paragraph class in a set and get the clicked element class as follows:您可以将每个 class 和相应的段落 class 存储在一个set中,并获取点击的元素 class 如下:

 let set = { 'chevron1':'.more-fix-broken-links', 'chevron2':'.more-spelling-grammar' } $('.chevron1, .chevron2').click(function(e) { $(set[e.target.className]).toggleClass('active'); });
 img { width: 10px }.chevron { cursor: pointer; }.chevron.active { transform: rotate(90deg); }.column { display: flex; flex-direction: column; }.more-fix-broken-links { max-width: 280px; display: none; }.more-spelling-grammar { max-width: 280px; display: none; }.more-fix-broken-links.active { display: block; height: 100%; }.more-spelling-grammar.active { display: block; height: 100%; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="column"> <div> <input id="fix-broken-links" type="checkbox"> <label for="fix-broken-links">Fix broken links</label> <img class="chevron1" src="https://supersede.space/chevron.svg" alt="Checklist chevron"> </div> <div> <p class="more-fix-broken-links">SKuska skuskaSKuska skuskaSKuska skuskaSKuska skuskaSKuska skuska</p> </div> </div> <div class="column"> <div> <input id="spelling-grammar" type="checkbox"> <label for="spelling-grammar">Spelling & grammar</label> <img class="chevron2" src="https://supersede.space/chevron.svg" alt="Checklist chevron"> </div> <div> <p class="more-spelling-grammar">SKuska skuskaSKuska skuskaSKuska skuskaSKuska skuskaSKuska skuska</p> </div> </div>

Try to use parent element too, like this.也尝试使用父元素,像这样。 On my example, using 2 .parent() brings you to a .column level, and from that point you start search of tag p using .find('p') .在我的示例中,使用 2 .parent()会将您带到.column级别,然后您开始使用.find('p')搜索标签p Or you can set a specific class to p element ant search for them.或者您可以将特定的 class 设置为p元素 ant 搜索它们。

 $('.chevron').click(function() { $(this).toggleClass('active'); // for you css.chevron.active $(this).parent().parent().find('p').toggleClass('active'); });
 img { width: 10px }.chevron { cursor: pointer; }.chevron.active { transform: rotate(90deg); }.column { display: flex; flex-direction: column; }.more-fix-broken-links { max-width: 280px; display: none; }.more-spelling-grammar { max-width: 280px; display: none; }.more-fix-broken-links.active { display: block; height: 100%; }.more-spelling-grammar.active { display: block; height: 100%; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="column"> <div> <input id="fix-broken-links" type="checkbox"> <label for="fix-broken-links">Fix broken links</label> <img class="chevron" src="https://supersede.space/chevron.svg" alt="Checklist chevron"> </div> <div> <p class="more-fix-broken-links">SKuska skuskaSKuska skuskaSKuska skuskaSKuska skuskaSKuska skuska</p> </div> </div> <div class="column"> <div> <input id="spelling-grammar" type="checkbox"> <label for="spelling-grammar">Spelling & grammar</label> <img class="chevron" src="https://supersede.space/chevron.svg" alt="Checklist chevron"> </div> <div> <p class="more-spelling-grammar">SKuska skuskaSKuska skuskaSKuska skuskaSKuska skuskaSKuska skuska</p> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM