简体   繁体   English

如何从href获取图像高度和宽度?

[英]how to get image height and width from href?

I tried to get the image height and width from a href link image, but no luck.我试图从 href 链接图像中获取图像的高度和宽度,但没有运气。 is there anyone know how to solve this problem?有谁知道如何解决这个问题?

here is the code:这是代码:

<a href="/images/int_kit_a_set.jpg" class="upload">
    <img src="/Assets/images/int_kit_a_set_thumb.jpg"
        ondragstart="return false" height="108" width="144" alt="" />
</a>

Your HTML:您的 HTML:

<a href="/images/int_kit_a_set.jpg" class="upload"><img src="/Assets/images/int_kit_a_set_thumb.jpg"  ondragstart="return false" height="108" width="144" alt="" /></a>

Javascript w/jQuery: Javascript w/jQuery:

var image = $("<img />").attr("src", $(".upload").attr("href"));
$(document).append(image);
alert(image.height());
alert(image.width());
image.remove();

I didn't test the javascript... but I'm not really sure your question is clear... so maybe this is what you wanted, maybe not.我没有测试 javascript ......但我不确定你的问题是否清楚......所以也许这就是你想要的,也许不是。

Essentially, I'm grabbing the HREF attribute of the link, creating a new image on the page with that url, measuring the image, and then removing it from the DOM.本质上,我正在获取链接的 HREF 属性,使用 url 在页面上创建一个新图像,测量图像,然后将其从 DOM 中删除。

You could add a style to the image which placed it off screen or something... but you'd have to mess with it to find something that worked cross-browser.您可以为图像添加一种样式,使其脱离屏幕或其他东西......但是您必须弄乱它才能找到可以跨浏览器工作的东西。 Some browsers don't consistently load images which aren't visible.某些浏览器不会始终加载不可见的图像。 I'm also not sure if you'd run into any timing issues with my script.我也不确定您是否会在我的脚本中遇到任何时间问题。

Putting it all together: alert( new Image().src = $('.upload').attr('href')).width );把它们放在一起: alert( new Image().src = $('.upload').attr('href')).width );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM