假设我有这些数据而我只想prependTo
一次,因为当我尝试prependTo
$('.entry-title')
到('.container)
时,每篇文章都显示两次。
<article class="blogpage-posts">
<div class="container">
<a href="#">Data One</a>
<h1 class="entry-title">Data to Prepend One</h1>
</div>
</article>
<article class="blogpage-posts">
<div class="container">
<a href="#">Data Two</a>
<h1 class="entry-title">Data to Prepend Two</h1>
</div>
</article>
这是代码......
$(document).ready(function(){
$('.entry-title').prependTo('.container');
});
这是使用该代码时的结果。
Data to Prepend One
Data to Prepend Two <--- the data the display twice
Data One
Data to Prepend Two
Data to Prepend One <--- the data the display twice
Data One Two
答案 0 :(得分:0)
使用此代码:
$(document).ready(function(){
$('.entry-title').each(function(){
$(this).prependTo($(this).closest('.container'));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<article class="blogpage-posts">
<div class="container">
<a href="#">Data One</a>
<h1 class="entry-title">Data to Prepend One</h1>
</div>
</article>
<article class="blogpage-posts">
<div class="container">
<a href="#">Data Two</a>
<h1 class="entry-title">Data to Prepend Two</h1>
</div>
</article>