2015-11-25 199 views
0

我需要更改父級iframe中div的backgroundImage。Javascript backgroundImage url css change

我可以做到這一點,但不是以有用的方式,我需要從按鈕發送變量。

目前我在腳本中有變量。

<script> 
function myFunction() { 
window.parent.document.getElementById('seekerBase').style.backgroundImage = "url('http://www.floom.com/images/waveform_lotos.gif')"; 
} 
</script> 

<button type="button" onclick="myFunction()">Get the background image of div</button> 

我需要從按鈕發送圖像URL,而不是在腳本中。

我試過這個,並沒有工作。

<script> 
    function myFunction() { 
    window.parent.document.getElementById('seekerBase').style.backgroundImage = "url()"; 
    } 
    </script> 

    <button type="button" onclick="myFunction('http://www.floom.com/images/waveform_lotos.gif')">Get the background image of div</button> 

感謝您的幫助。 我是新手。

+2

將它作爲參數'function myFunction(img){...}' – Jasen

回答

4

背景圖片URL http://www.floom.com/images/waveform_lotos.gif傳遞給myFunction(bgImgUrl)基本上是該函數的一個參數。 JavaScript函數的詳細信息,請參閱Mozilla/Functions

試試這個吧!

<script> 
    function myFunction(bgImgUrl) { 
    window.parent.document.getElementById('seekerBase').style.backgroundImage = 'url(' + bgImgUrl + ')'; 
    } 
</script> 

<button type="button" onclick="myFunction('http://www.floom.com/images/waveform_lotos.gif')">Get the background image of div</button> 

希望它有幫助!

+0

@showdev我做了。謝謝! – krishnaxv

+0

非常感謝krishnaxv,工作完美! –

+0

@Jürgen不客氣!如果確實有幫助,請**接受**作爲**答案**。謝謝! – krishnaxv