在中發(fā)現(xiàn)一元素ID為“placeholder”,JS中一函數(shù)為:var placeholder=document.getElementById("placeholder"); //placeholder為一<img>的IDplaceholder.setAttribute("src",source);后來發(fā)現(xiàn)在firefo和ie中出了問題 placeholder.setAttribute("src",source);這句話運行出了問題然后 我把那個元素ID改為了"m",其他代碼不變 ,結果就正確了!我猜測js中能直接根據(jù)ID獲取DOM元素,比如 ID.屬性(不知對不對),但是為啥ID與變量名重復就會出錯呢。。。沒懂源程序如下<!DOCTYPE html><html><head><meta http-equiv="content-type" content="text/html; charset=gb2312"/><title>圖片切換</title><style>body{font-family:"Helvetica","Arial",serif;color:#333;background-color:#ccc;margin:1em 10%;}h1{color:#c60;background-color:transparent;}a{color:#c60;background-color:transparent;font-weight:bold;text-decoration: none;}ul{padding:0;}li{float:left;padding:1em;list-style:none;}img{display:block;clear:both;}</style><script>Window.onload=showpic;function showPic(whichpic){ var source=whichpic.getAttribute("href"); var placeholder=document.getElementById("placeholder"); placeholder.setAttribute("src",source);alert("!!!"); var text=whichpic.getAttribute("title"); var description=document.getElementById("description"); description.childNodes[0].nodeValue=text;}</script></head><body><h1>Image Gallery</h1><ul> <li><a href="D:/網(wǎng)頁制作/Fireworks.jpg" onclick="showPic(this); return false;" title="A fireworks display">Fireworks</a></li> <li><a href="D:/網(wǎng)頁制作/Coffee.jpg" onclick="showPic(this); return false;" title="A cup of black coffee">Coffee</a></li> <li><a href="D:/網(wǎng)頁制作/Rose.jpg" onclick="showPic(this); return false;" title="A red, red rose">Rose</a></li> <li><a href="D:/網(wǎng)頁制作/Big_Ben.jpg" onclick="showPic(this); return false;" title="The famous clock">Big Ben</a></li></ul><img id="placeholder" src="D:/網(wǎng)頁制作/001.jpg" width="500px" heigth="300px" alt="my image gallery"/><p id="description">請選擇一張圖片.</p></body></html>僅僅想通過更換img的src屬性達到本頁更換圖片的效果
關于js中var的變量與ID重名
胡說叔叔
2019-03-14 18:19:08