【发布时间】:2014-05-13 04:37:51
【问题描述】:
我有一个 div (rateRibbon),我将其添加到随机数量的 父 div 中,每次父 (rateRibbon) 存在时都需要附加四个子 div。
<div class="ratesListing roomsView">
<div class="rateTypeLineItems"></div>
</div>
<div class="ratesListing roomsView">
<div class="rateTypeLineItems"></div>
</div>
与其向每个父级添加四个子 div,不如我如何循环直到达到 4,然后停止?
如何让每个新的 (rateRibbon) 及其 4 个子项附加到每个 rateTypeLineItems?
这是我的 JQuery:
//prepend rate ribbon at the top of each room rate type
$('.rateTypeLineItems').prepend('<div class="rateRibbon"> <!-- this is a container div --> </div>');
//prepend messaging container into rate ribbon
$('.rateRibbon').prepend('<div class="posMsg"></div><div class="posMsg"></div><div class="posMsg"></div><div class="posMsg"></div>');
//append messaging content every time "rateRibbon" loads
$('.rateRibbon:eq(0)').each(function (i) {
$('.posMsg:eq(0)').append('<h3>Best Price Guarantee</h3><p>Get the lowest price for our hotels. We Guarantee it.</p>');
$('.posMsg:eq(1)').append('<h3>IHG Rewards Club</h3><p>Be part of the world’s best hotel rewards program.</p>');
$('.posMsg:eq(2)').append('<h3>A Guaranteed Room</h3><p>Book directly with IHG to guarantee your room.</p>');
$('.posMsg:eq(3)').append('<h3>No Booking Fees</h3><p>There are no hidden booking fees when booking direct.</p>');
});
还有我的Fiddle的链接。
【问题讨论】:
-
一些建议:使用函数。如果您正在处理代码块,它们会让您的生活变得更轻松。
-
您可以将您的问题简化为您遇到的问题。有点难以跟上。仅显示问题期间出现的 HTML,并且仅显示相关的 jQuery。
标签: javascript jquery