javascript – 选择子元素jQuery

前端之家收集整理的这篇文章主要介绍了javascript – 选择子元素jQuery前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个这样的表格:
  1. <table>
  2. <tbody>
  3. for loop here
  4. <tr>
  5. <td>Item X Attribute 1</td>
  6. <td>Item X Attribute 2</td>
  7. <td><button name="test" onclick="display_variants()">Hide my kids</button></td>
  8. </tr>
  9. <tr>
  10. <tbody class="variants_info">
  11. <tr>
  12. <td>Item X Variant 1</td>
  13. <td>Item X Variant 2</td>
  14. </tr>
  15. </tbody>
  16. </tr>
  17. endfor loop
  18. </tbody>
  19. </table>

所以结构重复Y次.

我试图制作一个隐藏所选列的tbody.variants的脚本.

我到目前为止是这样的:

  1. <script>
  2. function display_variant(){
  3. if($('.variants_info').is(":visible")){
  4. $('.variants_info').hide();
  5. }
  6. else{
  7. $('.variants_info').show();
  8. }
  9. }
  10. </script>

但是这隐藏了所有行中的变体.

有没有办法选择特定行的孩子?还可以从隐藏的tbody.variants开始吗? (当我访问页面时开始).

编辑:目前正在密切关注这一点:

更新:
我设法改变了结构:

  1. <table>
  2. for loop
  3. <tbody>
  4. <tr>
  5. <td>image for el 1</td>
  6. <td>attr for el 1</td>
  7. <td><button type='button' name='test'>Click Me</button></td>
  8. </tr>
  9. for loop
  10. <tr class='variants_info'>
  11. <td>variant1 for el 1</td>
  12. <td>variant2 for el 1</td>
  13. </tr>
  14. endfor
  15. </tbody>
  16. endfor
  17. </table>

更新2:实际代码是这样的:

  1. <table class="pure-table pure-table-bordered">
  2. <tbody>
  3. {% for product in collection.products %}
  4. {% if product.available %}
  5.  
  6. <tr class="{% cycle 'pure-table-odd','' %}">
  7. <td>
  8. <img src="{{ product.featured_image.src | product_img_url: 'thumb' }}" alt="{{ product.featured_image.alt | escape }}" />
  9. </td>
  10. <td>{{ product.title }}</td>
  11. <td style="text-align:right">
  12. <button type="button" name="click_me">Click Me</button>
  13. </td>
  14. </tr>
  15. {% for variant in product.variants %}
  16. <tr>
  17. <td>{{variant.title}}</td>
  18. <td>{{variant.price | money }}</td>
  19. </tr>
  20. {% endfor %}
  21. {% endif %}
  22. {% endfor %}
  23. </tbody>
  24. </table>
  25.  
  26. <script>
  27. $("td button").click(function(){
  28. $(this).closest('tr').next().toggle();
  29. })
  30. </script>

我仍然无法让JS工作.. = /当前只是隐藏第一个变体(而不是所有的变体为点击的按钮)

解决方法

我建议标记包含具有类名称的产品的行,例如. “产品”,像这样:
  1. <tr class="product {% cycle 'pure-table-odd','' %}">
  2. <td>
  3. <img src="{{ product.featured_image.src | product_img_url: 'thumb' }}"
  4. alt="{{ product.featured_image.alt | escape }}" />
  5. </td>
  6. <td>{{ product.title }}</td>
  7. <td style="text-align:right">
  8. <button type="button" name="click_me">Click Me</button>
  9. </td>
  10. </tr>

您不会将该类添加到具有变体的行.

然后在JavaScript中使用nextUntil方法来匹配所有下一个变体行(没有“product”类),直到但不包括下一个产品行,并将toggle()方法应用于所有这些行:

  1. $("td button").click(function(){
  2. $(this).closest('tr').nextUntil('.product').toggle();
  3. });

替代结构1

您可以创建嵌套表,而不是单个表,每个表包含变体.看起来有点像这样:

  1. <table class="pure-table pure-table-bordered">
  2. <tbody>
  3. {% for product in collection.products %}
  4. {% if product.available %}
  5.  
  6. <tr class="{% cycle 'pure-table-odd','' %}">
  7. <td>
  8. <img src="{{ product.featured_image.src | product_img_url: 'thumb' }}"
  9. alt="{{ product.featured_image.alt | escape }}" />
  10. </td>
  11. <td>{{ product.title }}</td>
  12. <td style="text-align:right">
  13. <button type="button" name="click_me">Click Me</button>
  14. </td>
  15. </tr>
  16. <tr>
  17. <table class="some other class specific for variants">
  18. <tbody>
  19. {% for variant in product.variants %}
  20. <tr>
  21. <td>{{variant.title}}</td>
  22. <td>{{variant.price | money }}</td>
  23. </tr>
  24. {% endfor %}
  25. </tbody>
  26. </table>
  27. </tr>
  28. {% endif %}
  29. {% endfor %}
  30. </tbody>
  31. </table>

这具有优点和缺点.主要缺点是这些子表的列与主表中的列不对齐.但这取决于你想要什么…

然后,JavaScript代码必须像原来的那样:

  1. $("td button").click(function(){
  2. $(this).closest('tr').next().toggle();
  3. });

替代结构2

您还可以使用tbody标签来包装每个“部分”(由一个产品行和属于它的变体行组成).虽然不太常见,但这是允许的,如MDN所述:

Note that unlike the <thead>,<tfoot>,and <caption> elements however,multiple <tbody> elements are permitted (if consecutive),allowing the data-rows in long tables to be divided into different sections,each separately formatted as needed.

这与您的原始HTML不同,您将tbody元素作为tr元素的子代,这是无效的HTML.

看起来像这样:

  1. <table class="pure-table pure-table-bordered">
  2. {% for product in collection.products %}
  3. {% if product.available %}
  4.  
  5. <tbody>
  6. <tr class="{% cycle 'pure-table-odd','' %}">
  7. <td>
  8. <img src="{{ product.featured_image.src | product_img_url: 'thumb' }}"
  9. alt="{{ product.featured_image.alt | escape }}" />
  10. </td>
  11. <td>{{ product.title }}</td>
  12. <td style="text-align:right">
  13. <button type="button" name="click_me">Click Me</button>
  14. </td>
  15. </tr>
  16. {% for variant in product.variants %}
  17. <tr>
  18. <td>{{variant.title}}</td>
  19. <td>{{variant.price | money }}</td>
  20. </tr>
  21. {% endfor %}
  22. </tbody>
  23. {% endif %}
  24. {% endfor %}
  25. </table>

然后,您可以使用nextAll()方法隐藏变体行,因为它们由下一个产品行的tbody包装器分隔:

  1. $("td button").click(function(){
  2. $(this).closest('tr').nextAll().toggle();
  3. });

最初隐藏变体行

如果要首先隐藏所有变体,请将以下属性添加HTML代码中的这些行:

  1. <tr style="display:hidden">

你当然不会这样做的产品行.此外,您可能需要为此定义一个CSS类(例如tr.hidden {display:hidden;}而不是使用样式.

显示时,jQuery toggle()方法将覆盖此样式,并在隐藏时再次恢复.

猜你在找的jQuery相关文章