开发者

Get id from a one div to another div

开发者 https://www.devze.com 2022-12-25 14:46 出处:网络
Here 开发者_如何学Care the two divs <div id=\"1\"> <span id=\"a1\">first</span>

Here 开发者_如何学Care the two divs

<div id="1">

<span id="a1">first</span>
<span id="a2b">first</span>
<span id="a3">first</span>

</div>

Click

<div id=2></div>

And the jQuery code to do

  $('#2').bind('click',function(){
            var xx = $('#a'+i).attr('id');
            $('#2').append(xx);
            i=i+1;
        });

It does not get all the 3 id's from #1

Thanks Jean


First of all, element IDs should start with a letter or the underscore character. Second of all, try this:

$("#second").click(function() {

    // grab the ids of the first div's spans into an array
    var ids = $("#first span").map(function() {
        return this.id;
    }).get();
    alert(ids);

    $(this).append(ids.join(",")); // or whatever
});

See http://api.jquery.com/map/


You may try this:

 $('#2').bind('click',function(){
       var dest = $(this);
       $("#1").find("span").each(function(){
           dest.append($(this).attr('id'));
       });
  });


Hard to tell exactly what you want, but couldn't you do this to move all of the elements:

$("#1").children().appendTo("#2")


In your click function, variable 'i' is used before being set and this may explain your problems.

Can you check if initializing the variable helps?

0

精彩评论

暂无评论...
验证码 换一张
取 消