作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试验证用户上传的图片。因此,在用户添加其图像后,在将其上传到服务器之前,我尝试使用下面的 javascript 代码验证图像尺寸,但它不起作用。
function check_image_dimensions(){
var t=document.getElementById("main_file_1");
e=t.clientWidth;
n="";
o="We could not upload your image because \n \n";
if (e<300) {
(n+="The height of the image must be greater than 300px \n";
}
if(i<300){
n+="The height of the image must be greater than 300px \n";
}
我也试过:
e=t.width;
i=t.height;
但是还是不行。我做错了什么?
谢谢!
最佳答案
类似于:
1) 将图像放在隐藏的<img/>
中元素
请看这个例子:MDN - Using files from web applications
2) 检查 <img/>
的大小(边界框)元素
if(img.getBoundingClientRect().width<300) ...
3) 提醒用户(或不提醒)
这是一个工作版本:JSFiddle .
浏览器兼容性:火狐 3.6+, Chrome 7+,浏览器 10+,歌剧 12+
关于javascript - 上传前的图片高宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19824807/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!