2013-02-11 31 views
0
for (i=0;i<tar.length;i++) { 
document.write("<img src=" + tar[i] + " width='100' height='100'>"+" "); 
} 

焦油陣列具有圖像源,我想顯示使用的getElementById不是由文件撰寫在JS使用的getElementById圖像的顯示陣列

+2

這可能有所幫助:https://developer.mozilla.org/en-US/docs/DOM/document.createElement – 2013-02-11 19:44:56

回答

0

使用document.createElementappendChild,你可以這樣做:

var myContainer = document.getElementById("someContainerId"); 

for (i=0;i<tar.length;i++) { 
    var myimg = document.createElement("img"); 
    myimg.src= tar[i]; 
    myimg.width="100"; 
    myimg.height="100"; 

    myContainer.appendChild(myimg); 
} 

當然,請確保確實有一個容器元素,其ID爲someContainerId,或者任何您想要給它的ID。如果容器元素是<div>可能是最好的。

+0

你能給我發送完整的代碼嗎 – user1553487 2013-02-11 20:05:16

+0

我得到了一個錯誤,如「Uncaught TypeError:Can not call method 'appendChild'爲null「 – user1553487 2013-02-11 20:36:40

+0

@ user1553487錯誤或者是因爲在HTML中沒有'

',或者因爲您沒有[等待DOM加載](http://stackoverflow.com/questions/11163060 /不能設置屬性 - innerHTML的-的空/ 11163147#11163147)。 – apsillers 2013-02-11 20:43:27