『壹』 js如何获取图片显示时的大小尺寸
给img添加加载事件,图像加载完成才能获取图片的尺寸。
html中添加回onload
function showSize(img){
alert(img.width);
alert(img.height);
}
<div style="width:100px;height:600px"><img src="1.jpg" onload="showSize(this)"></div>
或者答js代码中添加onload
let img=document.querySelector("div img");
img.onload = function() {
alert(img.height);
alert(img.width);
}
『贰』 想做一个JS来获取图片的宽度跟高度
<img id="t" src="图片地址" />
<script>
var img = document.getElementById("t")
var temp = new Image();
temp.onload = function(){
var style = img.style,
ratio = Math.min( 1,
Math.max( 0, 100 ) / this.width || 1,
Math.max( 0, 100 ) / this.height || 1
);
//设置预览版尺权寸
style.width = Math.round( this.width * ratio ) + "px";
style.height = Math.round( this.height * ratio ) + "px";
}
temp.src = img.src;
</script>
『叁』 我想用js获取一张图片的宽度和高度 请问怎么能获取 图片在<img>标签中
||<img id="_img" src="abc.jpg" >
<script type="text/javascript">
function getWH(){
var id="_img";
var w=0;
var h=0;
// 具有较好的通用性内
var real= document.getElementById(id).height ||容 document.getElementById(id).style.height || document.getElementById(id).offsetHeight;
h=parseInt(real);
real= document.getElementById(id).width || document.getElementById(id).style.width || document.getElementById(id).offsetWidth;
w=parseInt(real);
alert("w:"+w+" ; h:"+h)
}
getWH();
</script>
『肆』 怎么用js获取图片宽高并写入html代码
<!DOCTYPEhtml>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metacharset="utf-8"/>
<title>获取Element中图片的属性</title>
</head>
<body>
<imgsrc="https://gss0.bdstatic.com/7051cy792sgCpNKfpU_Y_D3/static/common/widget/search-box-new/img/logo-new-101_bba4ab1.png"/>
<!--图片信息容器-->
<div></div>
<scripttype="text/javascript">
varimg=document.images[0];
vardiv=document.getElementsByTagName('div')[0];
//向div容器写入图片信息
div.innerHTML='图片的宽度是:'+img.width+'图片的高度是:'+img.height;
</script>
</body>
</html>
『伍』 js怎么获取 图片 的尺寸 和大小 并显示在html 里
(1)varimg=document.getElementById("img");
varwidth=img.getAttribute("width");
varheight=img.getAttribute("height");
document.getElementById("html").innerHTML("宽度:"+width,"高度:"+height);
(2)上述img为图片的id,html为需内要现在容在某个位置的id
『陆』 js 获取图片的高度和宽度问题
<script>
var image = new Image();
image.src = 'http://www..com/img/logo.gif';
image.onreadystatechange=function(){
if (image.readyState=="complete") {
alert(["图片大小是:",image.width,image.height]);
}
}
</script>
再试一下呢。。。
应该是图内片没有完全载入容吧
『柒』 js怎么获取div中图片的宽高,例如
亲,直接获取该图片后style.width就可以了,获取图片img用getElementsByTagName或者给img加个id用getElementsById获取
『捌』 js怎么获取 这个div内图片的宽度和
下面是代码,测试通过
<div style="width:100000px;" id="pal">
<img src="images/2009021121575533977.jpg" width="486" height="348" />
<img src="images/2009021121575533977.jpg" />
<img src="images/2009021121575533977.jpg" />
<img src="images/2009021121575533977.jpg" />
<img src="images/2009021121575533977.jpg" />
<img src="images/2009021121575533977.jpg" />
<img src="images/2009021121575533977.jpg" />
<img src="images/2009021121575533977.jpg" />
</div>
<SCRIPT>
var obj=document.getElementById("pal");
var imgs=obj.getElementsByTagName("img");
var sum=0;
for(var i=0;i<imgs.length;i++)
sum+=imgs[i].width;
alert(sum);
</SCRIPT>
『玖』 JS获取图片宽高,急急急~
var img = new Image();
img.onload = function(){
宽度=this.width
高度=this.height
}
img.src = 图片地址;
『拾』 js如何获取图片的高和宽根据我的部分代码添加完善,谢谢!
用我的这个代码你测试下!
<script>
var flag=false;
function DrawImage(ImgD,FitWidth,FitHeight){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= FitWidth/FitHeight){
if(image.width>FitWidth){
ImgD.width=FitWidth;
ImgD.height=(image.height*FitWidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
else{
if(image.height>FitHeight){
ImgD.height=FitHeight;
ImgD.width=(image.width*FitHeight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
}
}
</script>
---------------------------------------------------------------
调用:
<img border=0 src=123.jpg onload="javascript:DrawImage(this,500,400);">
其中:宽=500,高=400 可以修改.