2011-07-25 16 views
1

可以說我有一個形象的使用的代碼顯示:JS + HTML,得到SRC使用JavaScript

<img src="image.png"/> 

我想使用JavaScript來定義SRC,你會怎麼做這樣的事情

<script> 
function getImage() 
{ 
    return "image.png"; 
} 
</script> 
<img src="Javascript:getImage()"/> 

UPDATE:

我需要它與DivX播放器工作,通過JavaScript

獲取視頻網址paramater
<object classid="clsid:*************" width="320" height="260" codebase="http://go.divx.com/plugin/DivXBrowserPlugin.cab"> 
       <param name="custommode" value="none" /> 
       <param name="autoPlay" value="true" /> 
       <param name="src" value="Javascript:GetURL()" /> 
       <embed type="video/divx" src="Javascript:GetURL()" custommode="none" width="850" height="400" autoPlay="true" pluginspage="http://go.divx.com/plugin/download/"> 
       </embed> 
      </object> 

有沒有人有這個想法?

+1

也許這將幫助: http://stackoverflow.com/questions/116967/img-src-tags-and-javascript – Chris

回答

1

你應該給你的標籤各自的ID,然後做

src = 'image.png'; 
document.getElementById('param1').setAttribute('value', src); 
document.getElementById('embed1').setAttribute('src', src); 

<param id="param1" name="src" value="Javascript:GetURL()" /> 
       <embed id="embed1" type="video/divx" src="Javascript:GetURL()" custommode="none" width="850" height="400" autoPlay="true" pluginspage="http://go.divx.com/plugin/download/"> 
+0

我更新了我的問題,你可以看看它。 –

1

您必須將圖像分配一個ID,然後使用此:

document.getElementById('img_id').src="new_image.jpg"; 
0

您可以在嵌入代碼與動態寫帶有document.write()的視頻URL,但只能在頁面加載時執行此操作。