admin管理员组文章数量:1391975
i want to use onmouseover so that it replaces preview picture with thumbnail picture when mouse is on it.. below code works fine on firefox and IE but not working on chrome.. here is the link where it is applied samdesignli/gallery.html
<div class="gallery" align="center">
<h1>Photo Gallery</h1><br/>
<div class="thumbnails">
<img onMouseOver="preview.src=img1.src" id="img1" src="images/Salman_Siddiqui.jpg" alt="Image Not Loaded"/>
<img onMouseOver="preview.src=img2.src" id="img2" src="images/slide6.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img3.src" id="img3" src="images/slide1.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img4.src" id="img4" src="images/slide2.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img5.src" id="img5" src="images/slide3.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img6.src" id="img6" src="images/slide4.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img7.src" id="img7" src="images/slide5.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img8.src" id="img8" src="images/slide7.jpg" alt="Image Not Loaded"/>
</div><br/>
<div class="preview" align="center">
<img id="preview" src="images/Salman_Siddiqui.jpg" alt="No Image Loaded"/>
</div>
<br/>
</div>
i want to use onmouseover so that it replaces preview picture with thumbnail picture when mouse is on it.. below code works fine on firefox and IE but not working on chrome.. here is the link where it is applied samdesign.li./gallery.html
<div class="gallery" align="center">
<h1>Photo Gallery</h1><br/>
<div class="thumbnails">
<img onMouseOver="preview.src=img1.src" id="img1" src="images/Salman_Siddiqui.jpg" alt="Image Not Loaded"/>
<img onMouseOver="preview.src=img2.src" id="img2" src="images/slide6.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img3.src" id="img3" src="images/slide1.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img4.src" id="img4" src="images/slide2.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img5.src" id="img5" src="images/slide3.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img6.src" id="img6" src="images/slide4.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img7.src" id="img7" src="images/slide5.jpg" alt="Image Not Loaded"/>
<img onmouseover="preview.src=img8.src" id="img8" src="images/slide7.jpg" alt="Image Not Loaded"/>
</div><br/>
<div class="preview" align="center">
<img id="preview" src="images/Salman_Siddiqui.jpg" alt="No Image Loaded"/>
</div>
<br/>
</div>
Share
Improve this question
asked Jan 31, 2014 at 16:52
samsam
2463 gold badges7 silver badges17 bronze badges
1
- Uncaught TypeError: Cannot read property 'src' of undefined – Ruslan Commented Jan 31, 2014 at 16:54
4 Answers
Reset to default 1Try using getElementById
instead:
onmouseover="document.getElementById('preview').src=document.getElementById('img8').src"
Try this, but instead of calling each image ID just use "this".
onmouseover="document.getElementById('preview').src=this.src"
In some browsers, ids are automatically made into javascript variables. In others, like Chrome, they are not. It is bad practice to assume the variable exists for this exact reason. antyrat's answer should work for you.
If you want to avoid all the inline JavaScript in your code, I suggest you put it all in a separate <script>
tage or in its own js file.
Here's how your code would look like:
<h1>Photo Gallery</h1><br/>
<div class="thumbnails" id="thumbnails">
<img id="img1" src="https://www.google.ca/images/srpr/logo11w.png" alt="Image Not Loaded"/>
<img id="img2" src="images/slide6.jpg" alt="Image Not Loaded"/>
<img id="img3" src="images/slide1.jpg" alt="Image Not Loaded"/>
<img id="img4" src="images/slide2.jpg" alt="Image Not Loaded"/>
<img id="img5" src="images/slide3.jpg" alt="Image Not Loaded"/>
<img id="img6" src="images/slide4.jpg" alt="Image Not Loaded"/>
<img id="img7" src="images/slide5.jpg" alt="Image Not Loaded"/>
<img id="img8" src="images/slide7.jpg" alt="Image Not Loaded"/>
</div><br/>
<div class="preview" align="center">
<img id="preview" src="images/Salman_Siddiqui.jpg" alt="No Image Loaded"/>
</div>
<br/>
</div>
<script type="text/javascript">
var imgs = document.getElementById('thumbnails').getElementsByTagName('img');
var preview = function(e) {
document.getElementById('preview').src = document.getElementById(e.target.id).src;
};
for (var i=0, j=imgs.length; i<j; i++) {
imgs[i].addEventListener('mouseover', preview, false);
}
</script>
Edit: And if you use jQuery (since you tagged it), it's even easier:
$('#thumbnails > img').hover(function() {
$('#preview').attr('src', $(this).attr('src'));
});
本文标签: javascriptonmouseover not working on chromeStack Overflow
版权声明:本文标题:javascript - onmouseover not working on chrome? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744666345a2618554.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论