首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JQuery向嵌套元素添加元素

使用JQuery向嵌套元素添加元素
EN

Stack Overflow用户
提问于 2014-01-23 23:51:50
回答 2查看 37关注 0票数 0
代码语言:javascript
复制
I have a document structure that looks similar to this:

<article id="post1" class="post-5 page type-page" > 
   <section class = "holder clearfix">

     <div class = "four columns">
      <a title class = "single-image short-link" href "http://whatever.com" target = "self">
        </a>
     </div>

     <div class = "four columns">
      <a title class = "single-image short-link" href "http://whatever.com" target = "self">
        </a>
     </div>

     <div class = "four columns">
      <a title class = "single-image short-link" href "http://whatever.com" target = "self">
        </a>
     </div>

     <div class = "four columns">
      <a title class = "single-image short-link" href "http://whatever.com" target = "self">
        </a>
     </div>


 </section>
</article>

我想要做的是向每个添加一个带有类和唯一ID的div,一个使用JQuery的元素。

最终的结果会是这样的

代码语言:javascript
复制
<article id="post1" class="post-5 page type-page" > 
   <section class = "holder clearfix">

     <div class = "four columns">
      <a title class = "single-image short-link" href "http://whatever.com" target = "self">

           <div id="button1" class = "shiny-button"> </div>     // unique ID
        </a>
     </div>

     <div class = "four columns">
      <a title class = "single-image short-link" href "http://whatever.com" target = "self">

            <div id="button2" class = "shiny-button"> </div>     // unique ID
        </a>
     </div>

     <div class = "four columns">
      <a title class = "single-image short-link" href "http://whatever.com" target = "self">
            <div id="button3" class = "shiny-button"> </div>     // unique ID
        </a>
     </div>

     <div class = "four columns">
      <a title class = "single-image short-link" href "http://whatever.com" target = "self">     
          <div id="button4" class = "shiny-button"> </div>     // unique ID
        </a>
     </div>


 </section>
</article>

到目前为止,下面的JQuery代码将我带到一半的地方:

代码语言:javascript
复制
$('<div/>', {
    class:'shiny-button',
}).appendTo('.single-image');

我尝试使用类似于下面的代码,但我似乎无法正确地理解它

代码语言:javascript
复制
$('<div/>', {
    class:'shiny-button',
    id: 'button1',
}).appendTo('.holder .four:nth-child(1)');

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-23 23:57:07

试着用每一种。所以

代码语言:javascript
复制
$('.single-image').each(function(index){
    //Append new element to $(this)
    //index will be a counter
    //$(this) = the current $('.single-image') element
});

我相信你可以用这个索引来成为唯一的。我不记得了,但是.each()函数是在jQuery中记录的。

票数 0
EN

Stack Overflow用户

发布于 2014-01-24 00:11:57

试一试

代码语言:javascript
复制
$('#post1 .single-image').append(function(i){
    return '<div id="button'+(i+1)+'" class = "shiny-button"> dd</div>'
})

演示:小提琴

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21321769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档