当文档准备好并且调整窗口大小时,我正在使用 jquery 操作一些元素,问题是我不想在条件不适用时指定将所有内容设置回正常状态。这是我的代码:
function portfoliosingle_width(){
var window_width = $(window).width();
if(window_width < 964 ){
$('#portfolio-single #site-main .structure').width(450);
$('#portfolio-single #site-header').height(100);
$('#portfolio-single #site-header .structure').width(450);
$('#portfolio-single-text').width(425);
$('#portfolio-single-images').appendTo('.structure article');
$('#logo').css({'display':'block','width':'450px','float':'none'});
}else if(window_width > 964 ){
//want everything to go back the way it was without specifying each
}
}
$(document).ready(function(){
portfoliosingle_width();
});
$(window).bind("resize", function(){
portfoliosingle_width();
});
有没有一种方法可以在不查阅我的样式表并查找每个值并将每个值设置回原来的方式的情况下执行此操作?
最佳答案
而不是在 jquery 中指定宽度/高度,使用 css 类,通过添加一个类来完成,否则删除该类,它将恢复正常
$("#myelement").addClass("structure450");
然后在其他地方你可以做
//when you remove the class it will return to normal width and height
$("#myelement").removeClass("structure450");
或者您可以在 else 中将宽度和高度设置为“自动”。
看完你的评论,你也可以这样做。
//read the html in a variable
var orignalHTML = $("#parentDiv").html();
//in else use this html
else {
$("#parentDiv").html(orignalHTML);
}
关于javascript - 自动取消设置 jQuery 元素操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7382807/