第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關于js中var的變量與ID重名

關于js中var的變量與ID重名

胡說叔叔 2019-03-14 18:19:08
在中發(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屬性達到本頁更換圖片的效果
查看完整描述

1 回答

?
ibeautiful

TA貢獻1993條經(jīng)驗 獲得超6個贊

  1. 元素id直接作為變量名進入windows作用域(即進行了隱式聲明),引用對應的dom對象。因此默認情況下可以直接通過該變量名訪問dom對象。

  2. 題主的代碼單獨看沒有問題,原則上也不會出錯,顯式聲明的變量可以正常覆蓋隱式聲明的變量。

  3. 在沒有更多語境的情況下無法判斷題主遇到的問題是啥,建議提供具體出錯的類型以及上下文。


update:
注意變量名拼寫和大小寫:showpigshowPic

Window.onload=showpig;

function showPic(whichpic)

{

...

}


查看完整回答
反對 回復 2019-04-12
  • 1 回答
  • 0 關注
  • 320 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號