3

我正在开发一个项目,以便在选择缩略图并且工作正常时将缩略图图像交换为更大的图像但是,我现在希望最终用户能够单击更大的图像以在 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>
4

4 回答 4

3

好的。我得到了这个工作。'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>

感谢所有试图提供帮助的人。

于 2011-06-14T14:30:31.397 回答
0
var image = document.getElementById('image');
image.setAttribute('href', image.src);

应该管用。

于 2011-06-13T12:12:41.417 回答
0

更改此行

document.getElementById('image').setAttribute('href', this.src); 

到这里

document.getElementById('image').setAttribute('src', this.href); 
于 2011-06-13T12:15:32.730 回答
0

我认为您混淆了hrefandsrc属性。查看您的代码,我看到两件事:

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

应该管用。

于 2011-06-13T12:10:12.570 回答