简体   繁体   English

如何从 href 属性获取宽度和高度

[英]How can i get width and height from href attribute

截屏

i need to get this values using VANILLA javascript - 786 x 587 thanks我需要使用 VANILLA javascript 获取此值 - 786 x 587 谢谢

  • document.querySelector('a[href="link"]').clientWidth document.querySelector('a[href="link"]').clientWidth
  • document.querySelector('a[href="link"]').clientHeight document.querySelector('a[href="link"]').clientHeight

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 的widthheight的值

<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.

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