2015-02-09 17 views
0

我使用JavaScript創建一個新的窗口,在該窗口中創建元素, 當我嘗試使用ID使用該元素,我不能做到這一點,並採取空值使用JavaScript創建元素,並使用它

var unit_Window = window.open("", '',"height=400,width=400,status=yes"); 

unit_Window.document.write("<label id='title_ID'>title</label></center>"); 

現在,當我嘗試使用ID值回拿到冠軍值爲null

unit_Window.document.getElementById('title_ID'); 
+3

你正在使用'getElementById',它會回報你的整個元素本身不只是標題 – 2015-02-09 10:09:01

+1

嘗試'unit_Window.document.getElementById(「的title_id」)。innerHTML' – 2015-02-09 10:10:57

+0

我知道@NirbhayKundan但我意思是unit_Window.document.getElementById('title_ID')必須返回對象元素,但目前返回null – 2015-02-09 10:14:54

回答

0

我認爲你的問題的答案在於時機。在嘗試修改其內容之前,您需要確保新窗口已完成加載。
請參閱this post

丹尼

+0

我認爲,但我怎麼能知道彈出窗口我完成加載@allnodcoms – 2015-02-09 10:32:11

+0

@RafatHajAli快速谷歌搜索:http://stackoverflow.com/ a/2797574/2117156 – 2015-02-09 10:45:13

+0

@RafatHajAli - 檢查我在我的回答中發佈的鏈接,或上面的鏈接。 你只需要將你的DOM修改代碼放入一個回調函數中,當新窗口加載時就會調用它。 – allnodcoms 2015-02-10 09:15:36

0

爲了獲得一個元素的HTML

unit_Window.document.getElementById('title_ID').innerHTML; 

只有使用unit_Window.document.getElementById('title_ID');纔會返回HTML元素節點本身,而不僅僅是值。

要獲取元素的HTML值,請使用元素的innerHTML屬性。

+0

我知道@Murtaza Hussain,但我的意思是unit_Window.document。 getElementById('title_ID')必須返回對象元素,但是當前返回null – 2015-02-09 10:15:54

+0

如果是這樣的話,它的工作完美。 – 2015-02-09 10:16:46

+0

是的,我的情況不同@Murtaza Hussain – 2015-02-09 10:33:12

相關問題