onclick setAttribute href 来自 src
我正在开发一个项目,在选择缩略图时将缩略图图像交换为更大的图像,并且工作正常,但是,我现在希望最终用户能够单击更大的图像以在 jquery.lightbox 中显示它- 0.5。问题是选择缩略图后加载的图像没有 href。我希望可以使用 onclick 事件将图像 src 传递到 href,但我不知道如何实现这一点。
这是我的代码
<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>
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.
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好的。我得到了这个工作。 “a”缺少 id。
这是代码......
感谢所有试图提供帮助的人。
Ok. I got this working. The 'a' was missing an id.
Here's the code.....
Thank you to everyone who tried to help.
我认为您混淆了
href
和src
属性。查看您的代码,我看到两件事:document.getElementById('image').setAttribute('href', this.src);
//window.alert(document.getElementById('image').src);
您正在设置图像的 href 属性(应为 src),并从链接获取 src 属性(应为 href)。奇怪的是你在 window.alert 中是正确的
所以:
document.getElementById('image').setAttribute('src', this.href');
window.alert(document.getElementById('image').src
应该可以工作。
I think you're confusing the
href
andsrc
attributes. Looking at your code I see two things: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
So:
document.getElementById('image').setAttribute('src', this.href');
window.alert(document.getElementById('image').src
Should work.
应该有效。
Should work.
将此行更改
为这种方式
change this line
to this way