jQuery 只有“这个”。简单的问题
我只是陷入了一个简单的 jQuery 问题。 我正在尝试将每个 span
元素添加到 p
元素之前,但是 jQuery 将每个跨度重复 3 次(因为我有 3 个跨度元素)。好吧,这很难解释。
看看 Fiddle 代码 - http://jsfiddle.net/fTjcy/1/< /strong>
HTML:
<div class="events">
<div class="event">
<span class="red">31.08.11 - </span>
<p>...</p>
</div> <!-- END OF New -->
<div class="event">
<span class="red">28.08.2011 - </span><p>...</p>
</div> <!-- END OF New -->
<div class="event">
<span class="red">20.08.2011 - </span><p>...</p>
</div> <!-- END OF New -->
</div>
我想实现:
<div class="events">
<div class="event">
<p><span class="red">31.08.11 - </span>...</p>
</div> <!-- END OF New -->
<div class="event">
<p><span class="red">28.08.2011 - </span>...</p>
</div> <!-- END OF New -->
<div class="event">
<p><span class="red">20.08.2011 - </span>...</p>
</div> <!-- END OF New -->
</div>
jQuery:
$('.event span.red').hide().prependTo('.event p').show();
I am just stuck in one simple jQuery question.
I am trying to each span
element prependTo p
element, but jQuery is repeating each span 3 times (because I have 3 span elements). Ok it is hard to explain.
Just look at Fiddle code - http://jsfiddle.net/fTjcy/1/
HTML:
<div class="events">
<div class="event">
<span class="red">31.08.11 - </span>
<p>...</p>
</div> <!-- END OF New -->
<div class="event">
<span class="red">28.08.2011 - </span><p>...</p>
</div> <!-- END OF New -->
<div class="event">
<span class="red">20.08.2011 - </span><p>...</p>
</div> <!-- END OF New -->
</div>
I wanna achieve :
<div class="events">
<div class="event">
<p><span class="red">31.08.11 - </span>...</p>
</div> <!-- END OF New -->
<div class="event">
<p><span class="red">28.08.2011 - </span>...</p>
</div> <!-- END OF New -->
<div class="event">
<p><span class="red">20.08.2011 - </span>...</p>
</div> <!-- END OF New -->
</div>
jQuery:
$('.event span.red').hide().prependTo('.event p').show();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个
Try this
您只需定位当前的
span
,并将其添加到相对的p
前面。示例: http://jsfiddle.net/fTjcy/3/
You need to target the current
span
only, and prepend it to the relativep
only.Example: http://jsfiddle.net/fTjcy/3/