javascript 来自 src 的 onclick setAttribute href
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6330185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
onclick setAttribute href from a src
提问by Jason
I'm working on a project to swap thumbnail images to a larger image when the thumbnail is selected and it's working ok however, I would now like the end users to be able to click on the larger image to show it in jquery.lightbox-0.5. The problem is that after selecting the thumbnail the loaded image does not have a href. I was hoping that I could pass the images src to the href using an onclick event, but I can't figure out how to make this work.
I'm working on a project to swap thumbnail images to a larger image when the thumbnail is selected and it's working ok however, I would now like the end users to be able to click on the larger image to show it in jquery.lightbox- 0.5. 问题是选择缩略图后加载的图像没有href。我希望我可以使用 onclick 事件将图像 src 传递给 href,但我不知道如何进行这项工作。
Here's my code
这是我的代码
<script type="text/javascript">
function swaphref(image) {
document.getElementById('image').setAttribute('href', this.src);
//window.alert(document.getElementById('image').src);
}
</script>
<body>
<div class="productimage" id="gallery"><a onclick="swaphref(this)"><img id="image" img src="product_images/<?php echo $targetID; ?>/<?php echo $targetID; ?>.jpg" width="253" height="378" border="0" /></a></div>
</body>
回答by Jason
Ok. I got this working. The 'a' was missing an id.
行。我得到了这个工作。'a' 缺少一个 id。
Here's the code.....
这是代码......
<script type="text/javascript">
function swaphref(imagehref) {
document.getElementById('imagehref').setAttribute('href', image.src);
//window.alert(document.getElementById('image').src);
//window.alert(document.getElementById('imagehref').href);
}
</script>
<body>
<div class="productimage" id="gallery"><a href="#" id="imagehref" onclick="swaphref(this)"><img id="image" img src="product_images/<?php echo $targetID; ? >/<?php echo $targetID; ?>.jpg" width="253" height="378" border="0" /></a></div>
</body>
Thank you to everyone who tried to help.
感谢所有试图提供帮助的人。
回答by Erik Rothoff
I think you're confusing the href
and src
attributes. Looking at your code I see two things:
我认为你混淆了href
和src
属性。看看你的代码,我看到两件事:
document.getElementById('image').setAttribute('href', this.src); //window.alert(document.getElementById('image').src);
document.getElementById('image').setAttribute('href', this.src); //window.alert(document.getElementById('image').src);
You're setting the image's href-attribute, which should src, and your getting the src-attribute from the link, which should be href. The odd thing is that your correct in the window.alert
您正在设置图像的 href 属性,它应该是 src,并且您从链接中获取 src 属性,它应该是 href。奇怪的是你在 window.alert 中是正确的
So:
所以:
document.getElementById('image').setAttribute('src', this.href'); window.alert(document.getElementById('image').src
document.getElementById('image').setAttribute('src', this.href'); window.alert(document.getElementById('image').src
Should work.
应该管用。
回答by Pantelis
var image = document.getElementById('image');
image.setAttribute('href', image.src);
Should work.
应该管用。
回答by Vivek
change this line
改变这一行
document.getElementById('image').setAttribute('href', this.src);
to this way
以这种方式
document.getElementById('image').setAttribute('src', this.href);