Javascript 如何从href属性获取宽度和高度
我需要使用VANILLA javascript-786 x 587获得这个值 谢谢Javascript 如何从href属性获取宽度和高度,javascript,html,height,width,Javascript,Html,Height,Width,我需要使用VANILLA javascript-786 x 587获得这个值 谢谢 document.querySelector('a[href=“link”]”)。clientWidth document.querySelector('a[href=“link”]”)。clientHeight 以下是代码片段 function getImageHeight(link) { let img = document.createElement('img'); img.src = "http://pi
- document.querySelector('a[href=“link”]”)。clientWidth
- document.querySelector('a[href=“link”]”)。clientHeight
- 以下是代码片段
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)
}
我试着用正则表达式。在下面显示的情况下,
控制台.log
将返回URL的宽度
和高度
的值
<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>
var link=document.getElementById(“标记”).href;
//正则表达式的模式
var模式=/foo_uz(\d*)x(\d*).png/g
var result=newregexp(pattern).exec(link);
console.log(结果[1]);//返回第一个数字(宽度)
console.log(结果[2]);//返回第二位数字(高度)
在访问元素属性之前,需要缓存元素
var-element=document.getElementsByClassName('t')[0];document.write(element.getAttribute('href')代码>
结果:
您是否在href属性中提供了图像URL。可能的重复大小仍然不是我想要的大小。如果您使用唯一id,则更容易获取值,因为您不必获取数组索引