图片替换,全局变量的问题.谁能帮我解决。
<SCRIPT LANGUAGE= "JavaScript ">
var img1 = new Image();
var img2 = new Image();
img1.src = "http://farm1.static.flickr.com/56/189665365_cf72909146_t.jpg ";
img2.src = "http://farm1.static.flickr.com/49/189670801_6d8c6134f5_t.jpg ";
function switchimg(str)
{
var o = document.getElementById(str);
var other = (o.src.indexOf( "189670801_6d8c6134f5_t.jpg ") == -1)?img2:img1;
// alert(other.id);
other.id = str;
other.onclick = o.onclick;
o.replaceNode(other);
}
</SCRIPT>
<INPUT TYPE= "button " VALUE= " " NAME= "btn " ONCLICK= "switchimg( 'imglogo1 ') ">
<IMG SRC= "http://farm1.static.flickr.com/56/189665365_cf72909146_t.jpg " WIDTH= "132 " HEIGHT= "37 " BORDER= "0 " ALT= " " id=imglogo1 onclick= "alert(this.id) ">
<INPUT TYPE= "button " VALUE= " " NAME= "btn " ONCLICK= "switchimg( 'imglogo2 ') ">
<IMG SRC= "http://farm1.static.flickr.com/56/189665365_cf72909146_t.jpg " WIDTH= "132 " HEIGHT= "37 " BORDER= "0 " ALT= " " id=imglogo2 onclick= "alert(this.id) ">
--------------分割线----------------------------------
上面的程序实现图片不重载替换,但是出现了点击两个按钮出现对象丢失的情况。谁能帮我解决。怀疑是全局变量的问题。
------解决方案-------------------- <SCRIPT LANGUAGE= "JavaScript ">
var img1 = new Image();
var img2 = new Image();
img1.src = "http://farm1.static.flickr.com/56/189665365_cf72909146_t.jpg ";
img2.src = "http://farm1.static.flickr.com/49/189670801_6d8c6134f5_t.jpg ";
function switchimg(str)
{
var o = document.getElementById(str);
var otherTemp = (o.src.indexOf( "189670801_6d8c6134f5_t.jpg ") == -1)?img2:img1;
// alert(other.id);
var other=otherTemp.cloneNode(true)
other.id = str;
other.onclick = o.onclick;
o.replaceNode(other);
}
</SCRIPT>
<INPUT TYPE= "button " VALUE= " " NAME= "btn " ONCLICK= "switchimg( 'imglogo1 ') ">
<IMG SRC= "http://farm1.static.flickr.com/56/189665365_cf72909146_t.jpg " WIDTH= "132 " HEIGHT= "37 " BORDER= "0 " ALT= " " id=imglogo1 onclick= "alert(this.id) ">
<INPUT TYPE= "button " VALUE= " " NAME= "btn " ONCLICK= "switchimg( 'imglogo2 ') ">
<IMG SRC= "http://farm1.static.flickr.com/56/189665365_cf72909146_t.jpg " WIDTH= "132 " HEIGHT= "37 " BORDER= "0 " ALT= " " id=imglogo2 onclick= "alert(this.id) ">