I'm wondering if it's posible to switch positions of two divs with jQuery.
I have two div like this
so if div2 has content (or contains more than just white spaces) it switches the order of div1 and div2
so this:
would become this:
But if it was this:
or this:
it wouldn't do anything.
Also... if posible, if switched I would like to add a class to div1.
Any help with this will be very much appreciated.
UPDATE:
I forgot to add that I have to run this across multipul instanses on the same page.
Each instance is formated like this:
解决方案
I'll throw in my solution
$('.div2:parent').each(function () {
$(this).insertBefore($(this).prev('.div1'));
});
Edit: Doesn't work for whitespace in div2. Here's an updated solution:
$('.div2').each(function () {
if (!$(this).text().match(/^\s*$/)) {
$(this).insertBefore($(this).prev('.div1'));
}
});