'); $('#a').wr...">

Why can't I access my jQuery collection after it was used to wrap another item?

Look at this code ...

var b = $('<div id="b" />');
$('#a').wrap(b);
b.css({ border: '5px solid red' });

jsFiddle .

An item stored in bwill not have a border.

Is there a way to access bagain when it was used to transfer another item?

Or do I need again b = $('#b')?

Thank.

+3
source share
3 answers

I don't think jQuery actually uses the same instance of "b" to wrap it. You will need to overwrite "b" with the one that was created for the transfer.

var b = $('<div id="b" />');

b = $('#a').wrap(b).parent();

b.css({ border: '5px solid red' });

, , a ID, , .a.

b, .

+4

var b = $('');

$('# a'). wrap (b).css({border: '5px '});

+1

B contains an element in memory, after adding the DOM, you send b to the DOM in the second line and before calling css () the element referred to by "b", and not with the DOM matching.

+1
source

Source: https://habr.com/ru/post/1790602/


All Articles