[問題] getElementById 有辦法動態修改 id 嗎?

看板Ajax作者 (米虫三號)時間15年前 (2010/03/08 12:43), 編輯推噓1(103)
留言4則, 3人參與, 最新討論串1/2 (看更多)
最近想寫一個動態視窗,可以針對所點取的選項跳出說明視窗 只不過不知道為啥,如果只是單一跳脫視窗可以,但是如果很多利用變數去控制 就沒辦法跳出來,不知道是哪有問題 附上程式碼 我 showPopup 傳入三個變數 w, h, id <--- 這是要顯示哪一個 div 的屬性 function showPopup(w,h,id){ var popUp = document.getElementById("id"); popUp.style.top = "200px"; popUp.style.left = "200px"; popUp.style.width = w + "px"; popUp.style.height = h + "px"; if (baseText == null) baseText = popUp.innerHTML; popUp.innerHTML = baseText + "<div id=\"statusbar\"><button onclick=\"hidePopup(id);\">Close window</div>"; var sbar = document.getElementById("statusbar"); sbar.style.marginTop = (parseInt(h)-40) + "px"; popUp.style.visibility = "visible"; } <!-- div 我都用 hidden 讓其不顯示出來,要用再打開 --> <div id="popupcontent"> hello world </div> <div id="popupcontent2"> hello world2 </div> <!-- link --> <a href="#" onclick="showPopup(300,200,popupcontent);" >最新消息</a><br /> <a href="#" onclick="showPopup(300,200,popupcontent2);" >最新消息2</a><br /> 煩請各位大大解讀 <(_ _)> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 163.15.171.146

03/08 12:53, , 1F
var popUp = document.getElementById(id);
03/08 12:53, 1F

03/08 12:59, , 2F
一樣不行 無動於衷 XD
03/08 12:59, 2F

03/08 17:53, , 3F
要改的地方太多了 那就改天吧
03/08 17:53, 3F

03/14 18:46, , 4F
showPopup(300,200,'popupcontent');
03/14 18:46, 4F
文章代碼(AID): #1Bb81w6F (Ajax)
文章代碼(AID): #1Bb81w6F (Ajax)