简体   繁体   English

如何在jQuery中合并2个div?

[英]How to combine 2 divs in jquery?

Here is what I have. 这就是我所拥有的。 enter image description here #Image is working ok, but #act not. 在此处输入图片描述 #Image可以正常运行,但#act不能。 #act should write ACTION on bottom of every article(image) that meets requirment of Servlet (if data!=null). #act应该在符合Servlet要求的每个文章(图片)的底部写ACTION(如果data!= null)。 Unfortunately, I have ActionAction on bottom of first image. 不幸的是,我在第一张图片的底部有ActionAction。 What should I correct? 我应该纠正什么? And sorry for my bad English. 抱歉我的英语不好。

for(var i = 0; i<data.length; i++){                             
  $("#image").append(
    "<div class=divovi id="+data[i].sifra+">" +
    "<div><img width=150 height=150 border=2 align=middle id=\"slikaID\" src="+data[i].slika+" /></div>" +
    "<div>"+data[i].naziv+"</div><div id=act></div></div>"
  );
  $.post("http://localhost:8080/Projekat/DaLiJeNaAkcijiServlet",
    {
      data:JSON.stringify({ naslov:data[i].sifra})
    },
    function(data,status){
      if(data!=null) {
        $("#act").append('ACTION');
      }
    }   
  );
}

HTML ID's need to be unique, ie used once. HTML ID必须是唯一的,即只能使用一次。

If you change "<div>"+data[i].naziv+"</div><div id=act></div></div>"); 如果您更改"<div>"+data[i].naziv+"</div><div id=act></div></div>"); to "<div>"+data[i].naziv+"</div><div class=act></div></div>"); "<div>"+data[i].naziv+"</div><div class=act></div></div>"); (and update your CSS accordingly) you should then be able to use $(".act").append('ACTION'); (并相应地更新CSS),然后您应该可以使用$(".act").append('ACTION'); just once to append to all divs with that class - so be sure it's not in a loop otherwise many 'ACTIONS' may appear. 只需将一次附加到该类的所有div-因此请确保它不在循环中,否则可能会出现许多“ ACTIONS”。

An id is unique to only one element. id仅对一个元素唯一。 Use classes instead. 改用类。

for(var i = 0; i<data.length; i++){                             
                                $("#image").append(
                                        "<div class=divovi id="+data[i].sifra+">" +
                                        "<div><img width=150 height=150 border=2 align=middle id=\"slikaID\" src="+data[i].slika+" /></div>" +
                                        "<div>"+data[i].naziv+"</div><div class='act'></div></div>");   // id=act => class='act' 

                                $.post("http://localhost:8080/Projekat/DaLiJeNaAkcijiServlet",
                                        {

                                        data:JSON.stringify({
                                            naslov:data[i].sifra
                                        })
                                        },
                                        function(data,status){ 
                                            if(data!=null) {
                                                $(".act").append('ACTION'); // #act => .act

                                            }

                                        }           
                                );

                            }

To learn more about when to use Ids and when to use Classes check out this post. 要了解有关何时使用Id和何时使用Class的更多信息,请查看这篇文章。

You are putting an ajax request within the loop, which is really not recommended. 您将ajax请求放入循环中,实际上不建议这样做。 Unless you want your server to suffer from DDOS sympton sooner or later. 除非您希望服务器早晚遭受DDOS症状困扰。

You are also missing individual IDs for the act div, which is the main cause of your problem. 您还缺少行为div的个人ID,这是造成问题的主要原因。

for(var i = 0; i<data.length; i++){                             
  $("#image").append(
    "<div class=divovi id="+data[i].sifra+">" +
    "<div><img width=150 height=150 border=2 align=middle id=\"slikaID\" src="+data[i].slika+" /></div>" +
    "<div>"+data[i].naziv+"</div><div id='act+"i"'></div></div>"
  );
}
$.post("http://localhost:8080/Projekat/DaLiJeNaAkcijiServlet",
    {
      data: JSON.stringify({ naslov:data })
    },
    function(data,status){
      for(var i = 0; i<data.length; i++) {
         if(data[i].sifra!==null) {
           $("#act"+i).HTML('ACTION');
         }
      }
    }   
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM