无法将jQuery代码应用于具有相同类名的多个类?

toiithl6  于 2024-01-07  发布在  jQuery
关注(0)|答案(2)|浏览(253)

我有一些问题与应用jQuery代码在多个类具有相同的类名在一个页面上.它看起来像代码将只适用于一个类,但不适用于多个类.
正如您所看到的,页面上有两个TwoColumnUnit类,但只有第一个TwoColumnUnit获取jQuery代码,而没有获取第二个TwoColumnUnit类。
我如何修复代码,使这段代码将工作在多个类具有相同的类名?

  1. $('.TwoColumnUnit').each(function() {
  2. var pairs = [document.querySelector('.TwoColumnUnit_Left'), document.querySelector('.TwoColumnUnit_Right')];
  3. pairs.forEach(function(pair) {
  4. if (pair) {
  5. var boxes = pair.getElementsByClassName('col-sm-12 col-md-6');
  6. var maxHeight = 0;
  7. Array.from(boxes).forEach(function(box) {
  8. box.style.height = ''; // Reset
  9. maxHeight = Math.max(maxHeight, box.offsetHeight);
  10. });
  11. Array.from(boxes).forEach(function(box) {
  12. box.style.height = maxHeight + 'px';
  13. });
  14. }
  15. });
  16. });

个字符

yiytaume

yiytaume1#

querySelector()将只选择.TwoColumnUnit中的第一个匹配项。您应该改用querySelectorAll()
另外,我建议你避免混合使用JS和jQuery。

Demo:

  1. $('.TwoColumnUnit').each(function () {
  2. var pairs = [$(this).find('.TwoColumnUnit_Left'), $(this).find('.TwoColumnUnit_Right')];
  3. pairs.forEach(function (pair) {
  4. if (pair.length > 0) {
  5. var boxes = pair.find('.col-sm-12.col-md-6');
  6. var maxHeight = 0;
  7. boxes.each(function () {
  8. $(this).css('height', ''); // Reset
  9. maxHeight = Math.max(maxHeight, $(this).height());
  10. });
  11. boxes.css('height', maxHeight + 'px');
  12. }
  13. });
  14. });

个字符

展开查看全部
bwntbbo3

bwntbbo32#

因为您的DOM选择器选择相同的两个div
你想尽可能多地使用jQuery或者根本不使用
注意我不需要测试div是否存在,因为如果没有找到它们,脚本将忽略它们

  1. $('.TwoColumnUnit').each(function() {
  2. const $boxes = $(this).find('.newsLetterDiv > div'); // the divs right under newsLetter
  3. var maxHeights = $boxes
  4. .map(function() { // jQuery map
  5. return +this.offsetHeight; // the DOM property
  6. })
  7. .get(); // array
  8. const maxHeight = Math.max(...maxHeights); // find the tallest
  9. $boxes.each(function() {
  10. $(this).css({ "height": `${maxHeight}px` });
  11. });
  12. });

个字符
没有jQuery

  1. document.querySelectorAll('.TwoColumnUnit').forEach(unit => {
  2. const boxes = unit.querySelectorAll('.newsLetterDiv > div'); // the divs right under newsLetter
  3. const maxHeights = [...boxes].map(box => +box.offsetHeight);
  4. const maxHeight = Math.max(...maxHeights); // find the tallest
  5. boxes.forEach(box => box.style.height = `${maxHeight}px`);
  6. });
  1. <div class="TwoColumnUnit">
  2. <div class="col-md-6 col-xs-12 TwoColumnUnit_Left">
  3. <div class="newsLetterDiv">
  4. <div class="col-sm-12 col-md-6">
  5. <div class="left_Side">
  6. <i class="fas fas fa-route"></i>
  7. <h3>This is about Tennis</h3>
  8. </div>
  9. <div class="right_Side">
  10. <p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each.&nbsp;</span></p>
  11. </div>
  12. </div>
  13. </div>
  14. <div class="newsLetterDiv">
  15. <div class="col-sm-12 col-md-6">
  16. <div class="left_Side">
  17. <i class="fas fas fa-basketball"></i>
  18. <h3>This is about Basketball</h3>
  19. </div>
  20. <div class="right_Side">
  21. <p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
  22. </div>
  23. </div>
  24. </div>
  25. </div>
  26. <div class="col-md-6 col-xs-12 TwoColumnUnit_Right">
  27. <div class="newsLetterDiv">
  28. <div class="col-sm-12 col-md-6">
  29. <div class="left_Side">
  30. <i class="fas fas fa-basketball"></i>
  31. <h3>This is about Basketball</h3>
  32. </div>
  33. <div class="right_Side">
  34. <p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
  35. </div>
  36. </div>
  37. </div>
  38. <div class="newsLetterDiv">
  39. <div class="col-sm-12 col-md-6">
  40. <div class="left_Side">
  41. <i class="fas fas fa-route"></i>
  42. <h3>This is about Tennis</h3>
  43. </div>
  44. <div class="right_Side">
  45. <p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each.&nbsp;</span></p>
  46. </div>
  47. </div>
  48. </div>
  49. </div>
  50. </div>
  51. <div class="TwoColumnUnit">
  52. <div class="col-md-6 col-xs-12 TwoColumnUnit_Left">
  53. <div class="newsLetterDiv">
  54. <div class="col-sm-12 col-md-6">
  55. <div class="left_Side">
  56. <i class="fas fas fa-route"></i>
  57. <h3>This is about Tennis</h3>
  58. </div>
  59. <div class="right_Side">
  60. <p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each.&nbsp;</span></p>
  61. </div>
  62. </div>
  63. </div>
  64. <div class="newsLetterDiv">
  65. <div class="col-sm-12 col-md-6">
  66. <div class="left_Side">
  67. <i class="fas fas fa-basketball"></i>
  68. <h3>This is about Basketball</h3>
  69. </div>
  70. <div class="right_Side">
  71. <p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
  72. </div>
  73. </div>
  74. </div>
  75. </div>
  76. <div class="col-md-6 col-xs-12 TwoColumnUnit_Right">
  77. <div class="newsLetterDiv">
  78. <div class="col-sm-12 col-md-6">
  79. <div class="left_Side">
  80. <i class="fas fas fa-basketball"></i>
  81. <h3>This is about Basketball</h3>
  82. </div>
  83. <div class="right_Side">
  84. <p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
  85. </div>
  86. </div>
  87. </div>
  88. <div class="newsLetterDiv">
  89. <div class="col-sm-12 col-md-6">
  90. <div class="left_Side">
  91. <i class="fas fas fa-route"></i>
  92. <h3>This is about Tennis</h3>
  93. </div>
  94. <div class="right_Side">
  95. <p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each.&nbsp;</span></p>
  96. </div>
  97. </div>
  98. </div>
  99. </div>
  100. </div>

的字符串

展开查看全部

相关问题