jQuery:每场比赛仅appendTo一次

需要将 的内容抛给span .cat上面div,但是这个事件将全部抛给span .cateach div。


在出去的路上我得到


date category category2 category3

date category category2 category3

date category category2 category3

我需要吗


date category

date category2

date category3

$(document).ready(function() {

  $('.bdt-post-grid-category').wrapInner('<span class="bdt-post-grid-date cat"></span>');

  $('.bdt-post-grid-date.cat').appendTo($(".bdt-post-grid-meta"));

});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="bdt-post-grid-meta bdt-subnav bdt-flex-middle">

  <span class="bdt-post-grid-date">date</span>

</div>

<div class="bdt-post-grid-category bdt-position-small bdt-position-top-left">

  <a href="#" rel="category tag">category</a>

</div>


<div class="bdt-post-grid-meta bdt-subnav bdt-flex-middle">

  <span class="bdt-post-grid-date">date</span>

</div>

<div class="bdt-post-grid-category bdt-position-small bdt-position-top-left">

  <a href="#" rel="category tag">category2</a>

</div>


<div class="bdt-post-grid-meta bdt-subnav bdt-flex-middle">

  <span class="bdt-post-grid-date">date</span>

</div>

<div class="bdt-post-grid-category bdt-position-small bdt-position-top-left">

  <a href="#" rel="category tag">category3</a>

</div>


动漫人物
浏览 73回答 1
1回答

富国沪深

用于.each()单独处理每个元素,仅附加到前一个元素。$(document).ready(function() {&nbsp; $('.bdt-post-grid-category').wrapInner('<span class="bdt-post-grid-date cat"></span>');&nbsp; $('.bdt-post-grid-date.cat').each(function() {&nbsp; &nbsp; $(this).appendTo($(this).closest(".bdt-post-grid-category").prev());&nbsp; });});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div class="bdt-post-grid-meta bdt-subnav bdt-flex-middle">&nbsp; <span class="bdt-post-grid-date">date</span></div><div class="bdt-post-grid-category bdt-position-small bdt-position-top-left">&nbsp; <a href="#" rel="category tag">category</a></div><div class="bdt-post-grid-meta bdt-subnav bdt-flex-middle">&nbsp; <span class="bdt-post-grid-date">date</span></div><div class="bdt-post-grid-category bdt-position-small bdt-position-top-left">&nbsp; <a href="#" rel="category tag">category2</a></div><div class="bdt-post-grid-meta bdt-subnav bdt-flex-middle">&nbsp; <span class="bdt-post-grid-date">date</span></div><div class="bdt-post-grid-category bdt-position-small bdt-position-top-left">&nbsp; <a href="#" rel="category tag">category3</a></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5