[英]onclick setAttribute href from a 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>
好的。 我得到了这个工作。 'a' 缺少一个 id。
这是代码......
<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>
感谢所有试图提供帮助的人。
我认为您混淆了href
和src
属性。 查看您的代码,我看到两件事:
document.getElementById('image').setAttribute('href', this.src); //window.alert(document.getElementById('image').src);
您正在设置图像的href-attribute,应该是src,并从链接中获取src-attribute,应该是href。 奇怪的是你在 window.alert
所以:
document.getElementById('image').setAttribute('src', this.href'); window.alert(document.getElementById('image').src
应该管用。
var image = document.getElementById('image');
image.setAttribute('href', image.src);
应该管用。
更改此行
document.getElementById('image').setAttribute('href', this.src);
到这里
document.getElementById('image').setAttribute('src', this.href);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.