我正在寻找一个JS规则,删除一个div如果contenttheight <500
我已经得到了以下工作的图像,但需要用一个命令来替换图像调整大小,以删除一个div:
$(document).ready(function() {
function imageresize() {
var contentheight = $('#maincontainer').height();
if ((contentheight) < '500'){
$('.fluidimage').attr('src','images/big-sm.jpg');
} else {
$('.fluidimage').attr('src','images/big.jpg');
}
}
imageresize();//Triggers when document first loads
$(window).bind("resize", function(){//Adjusts image when browser resized
imageresize();
});
});
感谢所有的帮助
使用jQuery删除div
或任何DOM元素只是$(selector).remove();
这是你想要的吗?
$(document).ready(function() {
function imageresize() {
var contentheight = $('#maincontainer').height();
if (contentheight < 500){
$('.fluidimage').attr('src','images/big-sm.jpg');
} else {
$('.fluidimage').attr('src','images/big.jpg');
}
}
imageresize(); //Triggers when document first loads
$(window).bind("resize", function(){
//Adjusts image when browser resized
imageresize();
});
});
500
是数字,不是字符串。它不应该在引号中。
小提琴:http://jsfiddle.net/eSBUz/
使用each来遍历div并检查维度如何?这样你就有了每个调用上的DIV的引用。像这样:
$(".box").each(function (index) {
console.log( $(this).attr("width") );
w = Number( $(this).attr("width") );
if( w > 10 ){
$(this).remove();
}
});
http://jsfiddle.net/hamiltonlima/dDJcA/http://jsfiddle.net/Venugopal/eSBUz/2/
$(document).ready(function() {
function imageresize() {
var contentheight = $('#maincontainer').height();
if (contentheight < 250){
$('#maincontainer').css('background','blue');
} else {
$('#maincontainer').css('background','red');
}
}
imageresize(); //Triggers when document first loads
$(window).bind("resize", function(){
//Adjusts image when browser resized
imageresize();
});
});