Jquery change the Element Order

前端 未结 1 455
不知归路
不知归路 2020-12-10 15:24

How can I move each label in front of the input element they\'re next to using jQuery?

相关标签:
1条回答
  • 2020-12-10 15:48
    $('.select .classCheckBox label').each(function() {
      $(this).insertBefore( $(this).prev('input') );
    });
    

    DEMO


    A little explain

    • $('.select .classCheckBox label') select each label within each .classCheckBox

    • $(this) within loop point to label

    • .insertBefore() insert any element before the matched element that passed as argument

    • $(this).prev('input') points the input before label

    • so, $(this).insertBefore( $(this).prev('input') ) will insert each label before its previous input


    Related refs:

    • .insertBefore()

    • .prev()

    • .each()


    Alternate ways:

    $('.select .classCheckBox input').each(function() {
      $(this).insertAfter( $(this).next('label') );
    });
    

    DEMO

    OR

    $('.select .classCheckBox input').each(function() {
      $(this).before( $(this).next('label') );
    });
    

    DEMO

    0 讨论(0)
提交回复
热议问题