开发者

jQuery: how to I append $(this)

开发者 https://www.devze.com 2023-01-26 01:03 出处:网络
I have a jQuery method that I want to append the element $(this) to. My code is $(\"#ac开发者_JS百科tions ul\" ).append($(this));

I have a jQuery method that I want to append the element $(this) to.

My code is

$("#ac开发者_JS百科tions ul" ).append($(this)); 

However this does seem to do anything. Using the line

.append($(this).html()) 

does work, but it only grabs the child of $(this) (a div) not the whole element (an li).

I'm probably doing something moronic, hopefully someone can point this out to me.


Without knowing exactly what this refers to, the only reason I can think of for $(this).html() to work and not $(this) is that the former method creates a new element from the html code whereas appending $(this) will move the element.

See if the below fixes your problem:

$("#actions ul" ).append($(this).clone()); 


It's not that clear what $(this) refers to, but if it's the child element of the element you want then $(this).parent() should get the parent element you're after.


It sounds like this is set to a selector that finds the <div> instead of the <li>. You can either change the selector or, like miket2e suggests, use .parent() instead. Note: you should not have to use .html() as well since the .append function can take a HTML string or a jQuery object.

If you post the value of this, we could provide a more concrete answer.


Actually you want to use appendTo, this will grab the entire element.

$(this).appendTo("#actions ul");
0

精彩评论

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