在 div 上仅在项目前添加一次
prependTo item only once on div
假设我有这个数据,我只想 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
使用此代码:
$(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>
假设我有这个数据,我只想 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
使用此代码:
$(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>