Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 附加到div图像并计算它';s高度并设置为css_Javascript_Jquery - Fatal编程技术网

Javascript 附加到div图像并计算它';s高度并设置为css

Javascript 附加到div图像并计算它';s高度并设置为css,javascript,jquery,Javascript,Jquery,我有这样的js(我也使用jQuery): 但当我尝试获取图像大小时,我看到它的高度(根据我的代码)是0 如何附加、获取和设置css图像大小?我的代码不起作用加载图像后需要获取高度 $(".article_big_photo").click(function() { $('#screen').css({ opacity: 0.5, 'width':$(document).width(),'height':$(document).height()}); $('#screen').sh

我有这样的js(我也使用jQuery):

但当我尝试获取图像大小时,我看到它的高度(根据我的代码)是0


如何附加、获取和设置css图像大小?我的代码不起作用

加载图像后需要获取高度

$(".article_big_photo").click(function() {
    $('#screen').css({ opacity: 0.5, 'width':$(document).width(),'height':$(document).height()});
    $('#screen').show();
    $('#article_photo_modal').show(); 
    var image = document.createElement("img");
    var url = $(this).attr("b_img");
    $(image).attr("src", url); 
    $(image).load(function(){
        $('#article_photo_modal').css({ 'margin':$(image).height() +" 0 0 -270px"});
    }); 
    $('.photo_innerbox').append(image);   
});

加载图像后,需要获取高度

$(".article_big_photo").click(function() {
    $('#screen').css({ opacity: 0.5, 'width':$(document).width(),'height':$(document).height()});
    $('#screen').show();
    $('#article_photo_modal').show(); 
    var image = document.createElement("img");
    var url = $(this).attr("b_img");
    $(image).attr("src", url); 
    $(image).load(function(){
        $('#article_photo_modal').css({ 'margin':$(image).height() +" 0 0 -270px"});
    }); 
    $('.photo_innerbox').append(image);   
});

就像Arun说的,你必须在图像加载后获取高度

您也可以尝试以下方法:

var img = new Image();
img.src = $(this).attr("b_img");
img.height;

就像Arun说的,你必须在图像加载后获取高度

您也可以尝试以下方法:

var img = new Image();
img.src = $(this).attr("b_img");
img.height;
可能的重复可能的重复