jQuery - insertAfter不起作用

时间:2017-01-31 13:39:02

标签: javascript jquery html css

我试图将"Someone unsubscribed from the event"移到"在网站上使用jQuery获取报价" 按钮,我正在尝试在jQuery方面遇到麻烦。如果我从目标移除#whatsOnLink的附加部分,那么它将移动正常,但我需要它进入持有div。



.button

$('div[class^="grid"] a#whatsOnLink').each(
  function() {
    $(this).insertAfter($(this).closest('div[class^="grid_3"] div .button'));
  });




感谢任何帮助。

此致 基兰

2 个答案:

答案 0 :(得分:0)

使用此

$(this).closest('div[class^="grid_3"]').find('div .button')

而不是

$(this).closest('div[class^="grid_3"] div .button')



$(document).ready(function() {

  $('div[class^="grid"] a#whatsOnLink').each(
    function() {
      $(this).insertAfter($(this).closest('div[class^="grid_3"]').find('div .button'));
    });

});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="grid_3 alpha omega selected cheapest">
  <div class="cheapest-body">

    <div class="cheapest-heading">
      <p>BEST VALUE</p>
    </div>

    <a id="whatsOnLink" href="">Test</a>

    <p class="latest-type">Silver apartment
      <br>from <span class="latest-offer-price">£75</span>  <span>pp</span>  <a class="lightbox lightbox-accomodation-more-info lightbox-added" href="/accommodation-overlays/bognor-regis-silver-apartment.aspx"><span>More Information</span></a>
    </p>

    <a class="button bookingEngine button-red" href="">
      <span>Get A Quote</span>
    </a>

    <img src="" class="grey-out-overlay">

  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以使用.next()

$('div[class^="grid"] a#whatsOnLink').each(function() {
  $(this).insertAfter($(this).next().next());

  // OR

  // $(this).appendTo($(this).parent());
});

&#13;
&#13;
$('div[class^="grid"] a#whatsOnLink').each(function() {
  $(this).insertAfter($(this).next().next());
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="grid_3 alpha omega selected cheapest">
  <div class="cheapest-body">

    <div class="cheapest-heading">
      <p>BEST VALUE</p>
    </div>
    <a id="whatsOnLink" href="">Test</a>
    <p class="latest-type">Silver apartment
      <br>from <span class="latest-offer-price">£75</span>  <span>pp</span>  <a class="lightbox lightbox-accomodation-more-info lightbox-added" href="/accommodation-overlays/bognor-regis-silver-apartment.aspx"><span>More Information</span></a>
    </p>
    <a class="button bookingEngine button-red" href="">
      <span>Get A Quote</span>
    </a>
    <img src="" class="grey-out-overlay">
  </div>
</div>
&#13;
&#13;
&#13;