Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.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 在动态HTML中检测和调整图像的宽度和高度_Javascript_Jquery_Css - Fatal编程技术网

Javascript 在动态HTML中检测和调整图像的宽度和高度

Javascript 在动态HTML中检测和调整图像的宽度和高度,javascript,jquery,css,Javascript,Jquery,Css,我有一些来自RESTAPI的HTML。在这方面,我有很多标记,它们可能有宽度和高度属性集,也可能没有。这些单位不是“px”,而是一个数字 <img class="embedded-image" src="http://example.com/img.svg" height="150" width="200"> <img class="embedded-image" src="http://example.com/img.svg" height="150"> <

我有一些来自RESTAPI的HTML。在这方面,我有很多标记,它们可能有宽度和高度属性集,也可能没有。这些单位不是“px”,而是一个数字

<img class="embedded-image" src="http://example.com/img.svg" height="150" 
width="200">

<img class="embedded-image" src="http://example.com/img.svg" height="150">

<img class="embedded-image" src="http://example.com/img.svg" width="200">

<img class="embedded-image" src="http://example.com/img.svg">


使用CSS或JavaScript,我希望以这些“img”标记为目标,如果设置了,则使用px应用width属性,如果设置了,则设置height属性。如果没有设置它们,我们就保持原样。

假设您已经将它们添加到DOM中,您可以循环查看宽度和高度是否存在

var\u imgs=document.queryselectoral(“.embedded image”);
_imgs.forEach(功能(el){
如果(el.getAttribute(“高度”)!=null){
el.style.height=el.getAttribute(“高度”)+“px”;
}
如果(el.getAttribute(“宽度”)!=null){
el.style.width=el.getAttribute(“宽度”)+“px”;
}
});

hmm。。我得到一个错误“属性'style'在'Element'类型上不存在”,单位不在“px”中,而是作为一个数字。它被认为是像素,你不必做任何事情。。。。图像的固有高度,以像素为单位。必须是不带单位的整数。()