当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - How to select image src width - Jquery

问题描述:

How do I select the width of selected image on choose file?

if(new Image().attr('src').width() < 200){

alert('Expected');

}else{

alert('Woah?');

}

网友答案:

To select the source and width of an image using Javascript (not jQuery specific):

Here's a jsFiddle, and here's the js code:

var test_image = document.getElementById("TestImage");
var test_image_src = test_image.getAttribute("src"); // get the source
var test_image_width = test_image.getAttribute("width"); // get the width

if(test_image_width < 200){
    alert('Expected');
}else{
    alert('[Woah]?');
}

You can retrieve any attribute using this method.

var attribute = element.getAttribute(attributeName);
https://developer.mozilla.org/en-US/docs/Web/API/element.getAttribute

分享给朋友:
您可能感兴趣的文章:
随机阅读: