[問題] 新手請問 getElementById

看板Ajax作者 (woody)時間14年前 (2012/02/20 01:12), 編輯推噓2(205)
留言7則, 3人參與, 最新討論串1/1
新手正在學 Javascript 目前對 getElementById 有問題 該如何在想要的 div 裡執行function ===========我的作法============ Code 1 : http://tinyurl.com/78jmlon 執行結果 : http://tinyurl.com/6o6mpqf 按下按鈕後 :http://tinyurl.com/7uleber Code 2 :http://tinyurl.com/7ey2dg7 執行結果 : http://tinyurl.com/6o6mpqf 按下按鈕後 : http://tinyurl.com/89sp5yg ===============解釋=============== Code 1 使用 document.getElementById("aaa").innerHTML="I am innerHTML"; 按下按鈕後能如我所願在我要的區塊作改變 但Code 2 使用 document.getElementById("aaa").innerHTML=Q(); 結果卻始把整個頁面洗掉並且印出字 但我要的結果是想要在我想要改變的區塊做 function Q 請問該怎麼做才能在 div 中執行 funciton Q 非常謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.217.160.246

02/20 03:51, , 1F
先查一下document.write的用法吧
02/20 03:51, 1F

02/20 07:26, , 2F
function Q return string 就可以了
02/20 07:26, 2F

02/20 09:18, , 3F
document.write 不是這樣用的.....
02/20 09:18, 3F

02/20 09:20, , 4F
就是2F講的那樣,Q() 需要傳回值。如果沒傳回值,就會被當
02/20 09:20, 4F

02/20 09:20, , 5F
成空字串(還是undefine?) 於是你原本的字就被洗掉。
02/20 09:20, 5F

02/20 09:21, , 6F
然後,你的Q()又在HTML中寫入"I am fun.."那句,就變成你
02/20 09:21, 6F

02/20 09:22, , 7F
看到的那樣
02/20 09:22, 7F
文章代碼(AID): #1FGIrq7- (Ajax)