【问题标题】:Append child divs to each instance of parent将子 div 附加到每个父实例
【发布时间】: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>
  1. 与其向每个父级添加四个子 div,不如我如何循环直到达到 4,然后停止?

  2. 如何让每个新的 (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


【解决方案1】:

将您的每条消息存储在一个变量中,并将它们合并到 .rateRibbon 的包含中,这样它就会一次发生,您不必过多地查询 DOM。

这是小提琴:http://jsfiddle.net/x8VE5/

修改脚本的关键部分:

var msg1 = '<h3>Best Price Guarantee</h3><p>Get the lowest price for our hotels. We Guarantee it.</p>';
var msg2 = '<h3>IHG Rewards Club</h3><p>Be part of the world’s best hotel rewards program.</p>';
var msg3 = '<h3>A Guaranteed Room</h3><p>Book directly with IHG to guarantee your room.</p>';
var msg4 = '<h3>No Booking Fees</h3><p>There are no hidden booking fees when booking direct.</p>';

//prepend messaging container into rate ribbon
//var rateRibbon = '<div class="posMsg"></div>';
$('.rateRibbon').prepend('<div class="posMsg">'+msg1+'</div><div class="posMsg">'+msg2+'</div><div class="posMsg">'+msg3+'</div><div class="posMsg">'+msg4+'</div>');

【讨论】:

  • 哦是的!!!而已!我在一个新的演出中,经常遇到这种情况。我一定会尽可能地保存和重复使用它。虽然我确信我会在一段时间后理解它。谢谢,雷德莱娜!!
【解决方案2】:

有很多方法可以清理它,但从基础开始,遍历每个速率功能区 -

$('.rateRibbon').each(function (i) {
    $(this).prepend('<div class="posMsg"></div><div class="posMsg"></div><div class="posMsg"></div><div class="posMsg"></div>');
    $(this).find('.posMsg:eq(0)').append('<h3>Best Price Guarantee</h3><p>Get the lowest price for our hotels. We Guarantee it.</p>');
    $(this).find('.posMsg:eq(1)').append('<h3>IHG Rewards Club</h3><p>Be part of the world’s best hotel rewards program.</p>');
    $(this).find('.posMsg:eq(2)').append('<h3>A Guaranteed Room</h3><p>Book directly with IHG to guarantee your room.</p>');
    $(this).find('.posMsg:eq(3)').append('<h3>No Booking Fees</h3><p>There are no hidden booking fees when booking direct.</p>');
});

http://jsfiddle.net/jayblanchard/W25dw/1/

当然,您的代码可以进一步增强,具体取决于您想要做什么。

【讨论】:

  • 谢谢杰!这也是一个可靠的解决方案。我一定会保留并在将来使用它。
猜你喜欢
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 2014-06-14
  • 2015-07-15
  • 2016-03-31
  • 2013-01-28
  • 2012-12-20
  • 2014-09-10
相关资源
最近更新 更多