简体   繁体   中英

Wrapped context doesn't work properly

var parent = $('<div>parent</div>');
$('div').wrap(parent);

parent.append('<div>appended</div>');

I am testing it with below html code and append method doesn't work on parent object. Where i am doing mistake ?

<div>child</div>

Edit: Expected html result is

<div>parent
<div>child</div>
<div>appended</div>
</div>

You can do this:

$('div').wrap('<div>parent</div>').after('<div>appended</div>');

As every div is the only child of the wrapped around element, appending to the parent is the same as inserting the new element after the div .

DEMO

Try

 $(parent).html($(parent).html() + '<div>appended</div>');

Or

 $(parent).append($('<div></div>').html('appended'));

Also this might work

 $(parent).append($('<div>appended</div>'));

You caould try giving the child and ID:

HTML

<div id="child">child</div>

jQuery

var parent = '<div>parent</div>';
$('div').wrap(parent);

$('#child').parent().append('<div>appended</div>');

This works a treat, check it out - http://jsfiddle.net/ajthomascouk/MXbMQ/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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