javascript - 如何让图片库将显示图像更改为缩略图?

标签 javascript html css

我怎样才能让这个图片库把大图变成缩略图?我似乎无法让它工作。我需要将缩略图的 src 属性转换为大 src 属性值的值。

<html>
<head>
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="CSSChloe.css">

</head>
<body id = "bodyc">
    <div id = "space1">
    </div>
        <div id = "header">
        <div class="image">
        <img src = "Banner.png" alt= " " width="257" height="43" />
        <h4> text</h4>
        </div>
        </div>
        <div id = "space2"> </div>
        <div id= "menuBack">
            <div id= "menu1"><a href="index.html" style="text-decoration:none;"><p id = "menu2">link</p></a></div>
            <div id= "menu1"><a href="about.html" style="text-decoration:none;"><p id = "menu2">link</p></a></div>
            <div id= "select"><a href="photo.html" style="text-decoration:none;">           
            <img src = "back.png" alt = " " width = "257" height = "48" style = "position: absolute; left: -14px; top: -5px;" />
            <p id = "menu2" style = "position: absolute; margin-top: 2%;">Photo Gallery</p></a>
            </div>
            <div id= "menu1"><a href="price.html" style="text-decoration:none;"><p id = "menu2">link</p></a></div>
            <div id= "menu1"><a href="testimontials.html" style="text-decoration:none;"><p id = "menu2">link</p></a></div>
            <div id= "menu1"><a href="contact.html" style="text-decoration:none;"><p id = "menu2">link</p></a></div>
            <div id= "menu3"><img src ="photographylogo.png" width = "150" height = "125" /></div> 
        </div>
        <div id = "space3"> </div>
        <div id= "content">
        <div style = "width: 60%; height: 100%; float: left;">
        <img id = "big" src ="test1.png" width = "400" height = "350" style = "margin-top: 5%; margin-left: 8%;" />
        </div>
        <div style = "width: 35%; height: 100%; overflow: auto; float:right;">
        <img id = "thumb1" onclick = "thumbFunc('test2.png');" src = "test2.png" width = "150" height = "100" />
        <img id = "thumb2" src = " " width = "150" height = "100" />
        <br/>
        <img id = "thumb3" src = " " width = "150" height = "100" />
        <img id = "thumb4" src = " " width = "150" height = "100" />
        <br/>
        <img id = "thumb5" src = " " width = "150" height = "100" />
        <img id = "thumb6" src = " " width = "150" height = "100" />
        <br/>
        <img id = "thumb7" src = " " width = "150" height = "100" />
        <img id = "thumb8" src = " " width = "150" height = "100" />
        <br/>
        <img id = "thumb9" src = " " width = "150" height = "100" />
        <img id = "thumb10" src = " " width = "150" height = "100" />
        <br/>
        <img id = "thumb11" src = " " width = "150" height = "100" />
        <img id = "thumb12" src = " " width = "150" height = "100" />
        <br/>
        <img id = "thumb13" src = " " width = "150" height = "100" />
        <img id = "thumb14" src = " " width = "150" height = "100" />
        </div>
        </div>
        <script>
        function thumbFunc(a)
        {
        document.getElementById("big").setAttribute(src, a);
        }
        </script>
</body>
</html>

最佳答案

首先,更改您在内联点击时调用函数的方式:

onclick="thumbFunc('test2.png');"

到此为止,这样就不用再把图片路径放回去了:

onclick="thumbFunc(this.src)"

然后,更改您的 javascript:

document.getElementById("big").setAttribute(src, a);

为此(如果您更喜欢更简单的方式):

document.getElementById("big").src = a;

或者如果您仍想坚持使用 setAttribute 方法,请不要忘记用 " 符号将属性 (src) 括起来:

document.getElementById("big").setAttribute("src", a);

WORKING DEMO

关于javascript - 如何让图片库将显示图像更改为缩略图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23772883/

相关文章:

iframe 中的 JavaScript 不起作用

javascript - 从不同范围访问该函数 - 原生 js

javascript - 如何在关闭对话框后禁用它?

javascript - 使网页等待加载以识别框架id

html - 在不同的视口(viewport)上保持列高

javascript - 日期选择器上未捕获的 TypeError : $(. ..).datepicker 错误

css - 我的 2 列堆叠在 Zurb 中。如何并排显示它们?

javascript - 确保我每次按下鼠标都会得到一个鼠标弹起事件

html - 宝丽来照片/专栏在 Firefox 中无法正确显示

html - 如何在窗口调整大小时固定宽度?