2011-01-10 101 views
0
document.getElementById("myFrame").setAttribute("src") = "http://www.yahoo.com/"; 

myFrame是一個iframe元素...當進入這個開發人員控制檯,它給了我錯誤「賦值中無效的左側」我試圖更新iframe。有沒有我忘記的方法?簡單的javascript問題

+0

請參閱https://developer.mozilla.org/en/DOM/element.setAttribute – 2011-01-10 22:28:01

回答

0

在這裏,我固定它

document.getElementById("myFrame").setAttribute("src", "http://www.yahoo.com/"); 
3

setAttribute有兩個參數:

document.getElementById("myFrame").setAttribute("src", "http://www.yahoo.com/"); 

您正試圖將DOM對象設置爲字符串"http://www.yahoo.com/" ...這是無效的。

1

您不能分配的函數調用的東西,試試這個來代替:

document.getElementById("myFrame").setAttribute("src", http://www.yahoo.com/"); 
0

嘗試......

document.getElementById("myFrame").setAttribute("src","http://www.yahoo.com/"); 
+0

修復了您的拼寫錯誤。 – lonesomeday 2011-01-10 22:29:18

3

設置src屬性時,您並不需要setAttribute

document.getElementById('myFrame').src = 'http://www.yahoo.com/';