0

当窗口被调整大小时,我试图移动一些 div,如果 then 语句不应该这样工作:

$(window).resize(function() {
if ($(window).width() < 1024) {
    $('.div1').insertAfter('.div2');
}
else ($(window).width() > 1024) {
    $('.div1').insertBefore('.div2');
}
});

另外,我可以在使用 .resize 加载页面或首次加载页面时触发此操作吗?

4

2 回答 2

1

Else statements can't have a condition attached.

else ($(window).width() > 1024) {
    $('.div1').insertBefore('.div2');
}
});

has to be

else {
    $('.div1').insertBefore('.div2');
}
});

If you wan't to call this during page load, you can put it into a function and call the function both on resize and on page load.

于 2012-11-21T04:09:11.497 回答
1

尝试这个

$(window).resize(function() {
if ($(window).width() < 1024) {
    $('.div1').insertAfter('.div2');
}
else{
    $('.div1').insertBefore('.div2');
}
});

你可以使用jquery触发器

于 2012-11-21T04:11:51.837 回答