JS:访问随机分配的img属性

JS: accessing img attributes that were assigned randomly

本文关键字:img 属性 分配 随机 访问 JS      更新时间:2023-09-26

我有一个页面,上面有一块缩略图(小图像(,我希望在单击小图像时显示更大版本的图像。两个文件的名称相同,但位于不同的文件夹中。问题是,缩略图是使用for循环随机生成的。这是我正在编写的脚本(onclick(:

function seeFoto(that){
document.getElementById("photoViewer").style.zIndex="1";
document.getElementById("gallery").style.zIndex="0";
document.getElementById("bigpic").src=.../// <<-- problem: how do I get the source of the thumbnail and then replace "bigpic" with other version of the image??

以下是为每个单元格分配随机图像的脚本(它有效(:

function randomThumbs(){
swapp();
var onecell="1";
for (onecell=1; onecell<33; onecell++){
var duh=Math.ceil(Math.random()*802);
document.getElementById("img" +onecell).setAttribute("src","thumbs/"+duh+".jpg");
}}

swapp((是一个函数,它随机选择两个单元格,并将图片更改为另一个随机图片。Html代码:

<td><a id="link1" href="page.html" <img id="img1" src="thumbs/1.jpg" onclick="seeFoto(this)" /></a></td>
<td><a id="link2" href="page.html" <img id="img2" src="thumbs/2.jpg" onclick="seeFoto(this)" /></a></td>

希望我的问题足够清楚。。。谢谢

document.getElementById("bigpic").src = that.src.replace('thumbs/', 'bigimages/');

我想…:-(