[英]How can i get width and height from href attribute
here is the code snippet这是代码片段
function getImageHeight(link)
{
let img = document.createElement('img');
img.src = "http://picscdn.redblue.de/doi/pixelboxx-mss-76800921/fee_786_587_png/DURACELL-Duracell-BSC-4-db-AA-elem" || link
console.log(img.height)
console.log(img.width)
}
I tried using a regex.我尝试使用正则表达式。 in the case shown below the console.log
will return the values of the width
and height
of your URL在如下所示的情况下, console.log
将返回您的 URL 的width
和height
的值
<body>
<a id="tag" href="http://asdfasdfasdf.derp/foo_300x500.png"></a>
<script>
var link = document.getElementById("tag").href;
// pattern for the regex
var pattern = /foo_(\d*)x(\d*).png/g
var result = new RegExp(pattern).exec(link);
console.log(result[1]); // returns first digits (width)
console.log(result[2]); // returns second digits (height)
</script>
</body>
you need to cache the element before accessing its attributes您需要在访问其属性之前缓存该元素
var element = document.getElementsByClassName('t')[0]; document.write(element.getAttribute('href'));
<a class='t' href='value-im-looking-for'>link</a> <br> result:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.