我有以下代码:
var dateClasses = $('.dateClass'); // array of all your dateClasses
var contentClasses = $('.contentClass'); // array of all your contentClasses
for(var i=0; i< Math.min(dateClasses.length,contentClasses.length); i++) {
var frame = $("<div>", {class: "frame"}); // create your frame node
var timeLineDate = $("<span>",{class: "timeline-date"});
var timeLineContent = $("<div>",{class: "timeline-content"});
//Append DateClass to your timeline-data:
timeLineData.append(dateClasses[i]);
//Append ContentClass to your timeline-content:
timeLineContent.append(contentClasses[i]);
//Append all to your frame
frame.append(timeLineData);
frame.append(timeLineContent);
}
和
<span class="dateClass">date1</span>
<span class="dateClass">date2</span>
<span class="dateClass">date3</span>
<div class="contentClass">content1</div>
<div class="contentClass">content2</div>
<div class="contentClass">content3</div>
但是我不确定为什么它不起作用,任何建议? :/
感谢您的时间
修改
我向大家道歉,我提交的帖子太快了我完全应该得到赞成票。
我想做的确实(正如你提到的那样)将dateClass
元素移至timeline-date
,将contentClass
移至timeline-content
。但是,我需要一个frame
每dateClass
和contentClass
元素组:这意味着总共 3 frame
答案 0 :(得分:1)
我认为您要做的是将dateClass
元素移至timeline-date
和contentClass
移至timeline-content
。
但是在您的代码中,您正在创建新元素,但它们未添加到dom中,请尝试
var dateClasses = $('.dateClass'); // array of all your dateClasses
var contentClasses = $('.contentClass'); // array of all your contentClasses
$('.frame .timeline-date').append(dateClasses);
$('.frame .timeline-content').append(contentClasses);
演示:Fiddle
如果你想保持你的循环
var dateClasses = $('.dateClass'); // array of all your dateClasses
var contentClasses = $('.contentClass'); // array of all your contentClasses
var timeLineData =$('.frame .timeline-date');
var timeLineContent =$('.frame .timeline-content');
for (var i = 0; i < Math.min(dateClasses.length, contentClasses.length); i++) {
//Append DateClass to your timeline-data:
timeLineData.append(dateClasses[i]);
//Append ContentClass to your timeline-content:
timeLineContent.append(contentClasses[i]);
}
演示:Fiddle
答案 1 :(得分:1)
首先,你的问题不明确,应该做什么?
其次,您的代码上有一个拼写错误,您的var名称是“timeLineDate”,但您正在调用timeLineData两次。
<强>更新强>
<span class="dateClass">date1</span>
<span class="dateClass">date2</span>
<span class="dateClass">date3</span>
<div class="contentClass">content1</div>
<div class="contentClass">content2</div>
<div class="contentClass">content3</div>
var dateClasses = $('.dateClass'); // array of all your dateClasses
var contentClasses = $('.contentClass'); // array of all your contentClasses
var countElements = Math.min(dateClasses.length, contentClasses.length);
for(var i = 0; i < countElements; i++) {
var frame = $("<div>", {class: "frame"}); // create your frame node
var timeLineDate = $("<div>",{class: "timeline-date"});
var timeLineContent = $("<div>",{class: "timeline-content"});
//Append DateClass to your timeline-data:
timeLineDate.append(dateClasses[i]);
//Append ContentClass to your timeline-content:
timeLineContent.append(contentClasses[i]);
//Append all to your frame
frame.append(timeLineDate);
frame.append(timeLineContent);
$("body").append(frame);
}