Jquery 获取img height并单击添加到div height
我有一些可展开的长图像。我想指示Jquery在单击外部div时,将其高度扩展为图像的高度。如果有更好的办法,请告诉我。谢谢 HTML: JQuery:Jquery 获取img height并单击添加到div height,jquery,Jquery,我有一些可展开的长图像。我想指示Jquery在单击外部div时,将其高度扩展为图像的高度。如果有更好的办法,请告诉我。谢谢 HTML: JQuery: $(document).ready(function() { var imageLinks = $('a[href$=".png"], a[href$=".jpg"], a[href$=".JPEG"], a[href$=".gif"], a[href$=".bmp"]'); if (imageLinks.children('
$(document).ready(function() {
var imageLinks = $('a[href$=".png"], a[href$=".jpg"], a[href$=".JPEG"], a[href$=".gif"], a[href$=".bmp"]');
if (imageLinks.children('div').length) {
imageLinks.click(
function expand(e) {
e.preventDefault();
$(this).children('div').toggleClass('expanded');
$(document).scrollTop($(this).offset().top);
console.log('WHAT1'); /* console prints this */
/* code in question starts here */
$(this).children('div.project > img.sample').load(function() {
var pic_height = parseInt(this.height, 10);
console.log('WHAT2'); /* console does not print this */
$('.content').css('height', '+=' + pic_height);
});
}
);
}
});
请原谅我的代码太乱了,我对这方面还不太熟悉
谢谢 它不起作用,因为您使用变量
pic_height
作为css值的方式错误,请尝试$('.content').css('height','+='+pic_height)代码>这是有道理的,但它仍然给我:无法读取未定义的属性'load',不再出现控制台错误日志,但代码仍然无法工作。在顶部更新了Jquery。
.sample{
float: right;
height: auto;
width: auto;
}
$(document).ready(function() {
var imageLinks = $('a[href$=".png"], a[href$=".jpg"], a[href$=".JPEG"], a[href$=".gif"], a[href$=".bmp"]');
if (imageLinks.children('div').length) {
imageLinks.click(
function expand(e) {
e.preventDefault();
$(this).children('div').toggleClass('expanded');
$(document).scrollTop($(this).offset().top);
console.log('WHAT1'); /* console prints this */
/* code in question starts here */
$(this).children('div.project > img.sample').load(function() {
var pic_height = parseInt(this.height, 10);
console.log('WHAT2'); /* console does not print this */
$('.content').css('height', '+=' + pic_height);
});
}
);
}
});