如何在保留 addClass 和 removeClass 的情况下简化我的 jQuery 代码?

huangapple go评论67阅读模式
英文:

How can I simplify my Jquery code while keeping addClass and removeClass?

问题

每当我添加新的<li><img>元素时,我还必须在.js文件中添加新的代码行。

Codepen 这是在线代码的工作方式以及在编辑脚本后应继续工作的方式。

  1. // 鼠标进入
  2. $(".list li:nth-child(1)").on('mouseenter', function() {
  3. $(".img-display img.show").removeClass("show");
  4. $(".img-display img:nth-child(1)").addClass("show");
  5. })
  6. $(".list li:nth-child(2)").on('mouseenter', function() {
  7. $(".img-display img.show").removeClass("show");
  8. $(".img-display img:nth-child(2)").addClass("show");
  9. })
  10. $(".list li:nth-child(3)").on('mouseenter', function() {
  11. $(".img-display img.show").removeClass("show");
  12. $(".img-display img:nth-child(3)").addClass("show");
  13. })
  14. // 鼠标离开
  15. $(".list li:nth-child(1)").on('mouseleave', function() {
  16. $(".img-display img.show").addClass("show");
  17. $(".img-display img:nth-child(1)").removeClass("show");
  18. })
  19. $(".list li:nth-child(2)").on('mouseleave', function() {
  20. $(".img-display img.show").addClass("show");
  21. $(".img-display img:nth-child(2)").removeClass("show");
  22. })
  23. $(".list li:nth-child(3)").on('mouseleave', function() {
  24. $(".img-display img.show").addClass("show");
  25. $(".img-display img:nth-child(3)").removeClass("show");
  26. })

我希望能够通过保持addClassremoveClassmouseentermouseleave来简化我的代码,而不必每次添加新元素时都添加另一行代码。

目前,我只有这个来标识列表元素:

  1. $(".list li").on('mouseenter', function() {
  2. var index = $(this).index()
  3. })
英文:

Every time I add a new <li> and <img> element I also have to add a new line of code in the .js file

Codepen Here is the online code of how it works and how it should continue to work after editing the script.

  1. //Mouseenter
  2. $(".list li:nth-child(1)").on('mouseenter', function() {
  3. $(".img-display img.show").removeClass("show");
  4. $(".img-display img:nth-child(1)").addClass("show");
  5. })
  6. $(".list li:nth-child(2)").on('mouseenter', function() {
  7. $(".img-display img.show").removeClass("show");
  8. $(".img-display img:nth-child(2)").addClass("show");
  9. })
  10. $(".list li:nth-child(3)").on('mouseenter', function() {
  11. $(".img-display img.show").removeClass("show");
  12. $(".img-display img:nth-child(3)").addClass("show");
  13. })
  14. //Mouseleave
  15. $(".list li:nth-child(1)").on('mouseleave', function() {
  16. $(".img-display img.show").addClass("show");
  17. $(".img-display img:nth-child(1)").removeClass("show");
  18. })
  19. $(".list li:nth-child(2)").on('mouseleave', function() {
  20. $(".img-display img.show").addClass("show");
  21. $(".img-display img:nth-child(2)").removeClass("show");
  22. })
  23. $(".list li:nth-child(3)").on('mouseleave', function() {
  24. $(".img-display img.show").addClass("show");
  25. $(".img-display img:nth-child(3)").removeClass("show");
  26. })

I wanted to be able to simplify my code by keeping addClass, removeClass, mouseenter & mouseleave and not having to add another line of code every time I add new elements.

At the moment I only have this to identify the elements of the list:

  1. $(".list li").on('mouseenter', function() {
  2. var index = $(this).index()
  3. })

答案1

得分: 1

以下是翻译好的部分:

"Anyway you just need a little reasoning for this problem. Here's all you need:

  1. const lis = $('.list li');
  2. lis.on('mouseenter', function() {
  3. const index = lis.index(this);
  4. $('.img-display img.show').removeClass('show');
  5. $('.img-display img').eq(index).addClass('show');
  6. })
  7. ```"
  8. <details>
  9. <summary>英文:</summary>
  10. Anyway you just need a little reasoning for this problem. Here&#39;s all you need:
  11. ```javascript
  12. const lis = $(&quot;.list li&quot;)
  13. lis.on(&#39;mouseenter&#39;, function() {
  14. const index = lis.index(this)
  15. $(&quot;.img-display img.show&quot;).removeClass(&quot;show&quot;);
  16. $(&quot;.img-display img&quot;).eq(index).addClass(&quot;show&quot;)
  17. })

huangapple
  • 本文由 发表于 2023年2月19日 18:18:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定