admin管理员组文章数量:1393850
I know it's such a beginner thing. So I have this image in a div with the id thumb.
<img id="thumb" src="https://url-to-a-image">
And this Javascript that it's a magnify script:
<script type="text/javascript">
var myImgSrc = document.getElementById("thumb").getElementsByTagName("img")
[0].src;
var evt = new Event(),
m = new Magnifier(evt);
m.attach({
thumb: '#thumb',
large: 'myImgSrc',
largeWrapper: 'preview'
});
</script>
As you can see I'm trying to get the image using myImgSrc and then I'm trying to use in the large: 'myImgSrc'. When I put the a fixed url in large: fixed-url-to-the-image, it works fine.
I know it's such a beginner thing. So I have this image in a div with the id thumb.
<img id="thumb" src="https://url-to-a-image">
And this Javascript that it's a magnify script:
<script type="text/javascript">
var myImgSrc = document.getElementById("thumb").getElementsByTagName("img")
[0].src;
var evt = new Event(),
m = new Magnifier(evt);
m.attach({
thumb: '#thumb',
large: 'myImgSrc',
largeWrapper: 'preview'
});
</script>
As you can see I'm trying to get the image using myImgSrc and then I'm trying to use in the large: 'myImgSrc'. When I put the a fixed url in large: fixed-url-to-the-image, it works fine.
Share Improve this question edited Aug 7, 2018 at 15:46 Zakaria Acharki 67.5k15 gold badges78 silver badges106 bronze badges asked Aug 7, 2018 at 15:41 danskidanski 3291 gold badge6 silver badges17 bronze badges 1-
4
Try like this,
var thumb = document.getElementById("thumb").src;
? – Bhavin Commented Aug 7, 2018 at 15:43
4 Answers
Reset to default 5The element with #thumb
id is the tag img
it self, the current selector will not return the src
value, so it should be simply:
var myImgSrc = document.getElementById("thumb").src;
You can get image src
like this,
var thumb = document.getElementById("thumb").src;
You don't need to use getElementsByTagName
.
let img = document.querySelector('#thumb');
console.log(img.src);
If you use img.src, you'll see the source of your img tag.
getElementsByTagName
is superfluous - you already have the exact element you want - you selected it by its ID. You'd only need getElementsByTagName
if you wanted to get one or more elements by their tag and work on them all, rather than identifying one precisely.
So actually the solution is very simple - just get the src
attribute of the ID-selected element directly. Working demo:
var myImgSrc = document.getElementById("thumb").src;
console.log(myImgSrc);
<img id="thumb" src="https://url-to-a-image">
本文标签: magnifierjsHow to get a the url of an image from id in JavascriptStack Overflow
版权声明:本文标题:magnifier.js - How to get a the url of an image from id in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744084426a2588249.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论