简体   繁体   中英

Javascript document[“someid”].src works in Firefox, but not in other browsers

I'm trying to do very simple Javascript implementation.

It's with onMouseOver event over diferrent texts and that change one image.

It works perfectly on Firefox, but not in any other browsers.

HTML

<div id="foofoo1" onMouseover="imagehover(this);">display image1</div>
<div id="foofoo2" onMouseover="imagehover(this);">display image2</div>
<img id="foo" src="dir/images/foofoo1.jpg">

Javascript

function imagehover(idfoo){
  document["foo"].src = "dir/images/"+idfoo.id+".jpg";
}

That's a non-standard way of doing it anyway.

document.getElementById("foo").src = "dir/images/"+idfoo.id+".jpg";

This is the correct way to do it.

使用setAttribute Ref

document.getElementById("foo").setAttribute('src',"dir/images/"+idfoo.id+".jpg");

document.getElementById("foo")是否起作用(而不是document["foo"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM