我正在使用html和css创建一个简单的图库页面。我把它编码到一个html查看器,它工作正常,但是当我在谷歌浏览器和IE浏览器打开它时onmouseover无法正常工作。当您浏览选定的缩略图时,它应该在底部框中显示较大的图像,但它不起作用。
我之前发过这个问题并得到了非常好的回复。我想问一下,如果有人可以请给我一个PLAIN javascript代码,我需要投入使其完成这项工作。我有这个代码,有人亲切地给出了哪些有效,但是如果可能的话,我想要一个只有java脚本的代码。
$(function(){
$('.thumbnails img').hover(function(){
$('#preview').attr('src',$(this).attr('src'));
},null);
});
谢谢大家!
<!DOCTYPE html>
<head>
<title>Gallery</title>
<style type="text/css">
body {
background: #222;
margin-top: 100px;
}
h3 {
color: #eee;
font-family: Verdana;
}
.thumbnails img {
height: 100px;
border: 4px solid #555;
padding: 1px;
margin: 0 10px 10px 0;
}
.thumbnails img:hover {
border: 4px solid #00ccff;
cursor:pointer;
}
.preview img {
border: 4px solid #444;
padding: 1px;
width: 800px;
}
</style>
</head>
<body>
<div class="gallery" align="center">
<div class="thumbnails">
<img onmouseover="preview.src=img1.src" id="img1" src="http://i60.tinypic.com/2qjj62b.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img2.src" id="img2" src="http://i60.tinypic.com/mb4c21.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img3.src" id="img3" src="http://i61.tinypic.com/35avvpw.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img4.src" id="img4" src="http://i60.tinypic.com/29qnjme.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img5.src" id="img5" src="http://i62.tinypic.com/zkmvd2.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img6.src" id="img6" src="http://i61.tinypic.com/oqezus.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img7.src" id="img7" src="http://i57.tinypic.com/1tx6oj.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img8.src" id="img8" src="http://i58.tinypic.com/143onsj.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img9.src" id="img9" src="http://i61.tinypic.com/2l16qf.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img0.src" id="img0" src="http://i61.tinypic.com/21l0own.jpg" alt="Image Not Loaded"/>
</div></br>
<div class="preview" align="center">
<img id="preview" src="http://i60.tinypic.com/2qjj62b.jpg" alt="No Image Loaded"/>
</div>
</br>
</div>
</body>
</html>
答案 0 :(得分:0)
以下是您图库的vanilla JS版本:
var images = document.getElementsByClassName('thumbnails')[0].getElementsByTagName('img');
for (var i = 0; i < images.length; i++) {
images[i].onmouseover = function () {
document.getElementById('preview').src = this.src;
}
}
在此处查看:http://jsfiddle.net/2AM2N/5/
更新
以下是如何使用alt属性的描述:
document.getElementById('desc').innerHTML = this.alt; // this goes inside our loop