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?
精彩评论