English 中文(简体)
jQuery 基础 : 如何创建与此结构循环的循环?
原标题:jQuery basic: How to create a loop with this structure?

这是一个很简单的问题, 但我不知道如何在互联网上搜索。

我有一个空的 , 最终需要以下列方式查找 :

<div id="wrap>
    <div class="container">
        <div class="apples"></div>
    </div>
    <div class="container">
        <div class="banana"></div>
    </div>
    <div class="container">
        <div class="orange"></div>
    </div>
    <div class="container">
        <div class="grapes"></div>
    </div>
</div>

我有:

$(#wrap).html(  ); // need this bit
var fruitArray = [ apples , banana , orange , grapes ];
for (fruit in fruitArray) {
    $( <div class=" +fruitArray[fruit]+ "></div> ).appendTo( #wrap ).doSomething(); 
}

因为我需要与水果阵列班的Div做点什么, 我不能只是把它包在集装箱班上:

$( <div class="container"><div class=" +fruitArray[fruit]+ "></div></div> ).appendTo...

在这种情形下,我怎么能去制造集装箱类呢?

最佳回答

< a href=" "http://api.jquery.com/ wrap/" rel="no follow" >. wrap 方法返回用于链锁目的的原始元素组 。

$( <div class=" +fruitArray[fruit]+ " /> )
.appendTo( #wrap )
.wrap( <div class="container" /> )
.doSomething(); 
问题回答

尝试此 :

var fruitArray = [ apples , banana , orange , grapes ];
for (fruit in fruitArray) {
    $( <div class="  + fruitArray[fruit] +  "></div> ).appendTo( #wrap ).wrap( <div class="container"></div> );
}

example fiddle

var fruitArray = [ apples , banana , orange , grapes ];
for (fruit in fruitArray) {
    $( <div class=" +fruitArray[fruit]+ " /> ).wrap( <div class="container" /> ).appendTo( #wrap ).doSomething();
}

OR 也可以像

var fruitArray = [ apples , banana , orange , grapes ];
for (fruit in fruitArray) {
    $( <div class=" +fruitArray[fruit]+ " /> ).appendTo( #wrap ).doSomething().wrap( <div class="container" /> );
// this will work if the doSomething is a jquery/plugin method that is using chaining (usually they do)
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签