我試圖在用戶將光標懸停在選定的選項上時處理背景圖像。我在螢火蟲中測試了我的代碼並加載了頁面。瞬間,我得到:如何解決'Typerror未定義'消息的javascript中的函數?
TypeError: document.getElementsByTagName(...).style is undefined
var bg = document.getElementsByTagName("html").style.backgroundColor = "white";
和懸停我得到:
TypeError: bg.document is undefined
bg.document.getElementsByTagName("html").style.backgroundImage = "url('http://upload.wikimedia.org/wikipedia/commons/b/be/500_USD_note%3B_series_of_1934%3B_obverse.jpg')";
如何解決這個問題?
HTML代碼:
<form>
<!--show a large print of green font color and size money-->
Select your DSLR budget range:
<select id="budgetSel" onChange="onChange();">
<option value="selectOneBudget" selected="selected">Select one</option>
<option value="fiveHundredDollars" onMouseOver="changeBackgroundImage(this);">< $500</option>
<option value="thousandDollars" onMouseOver="changeBackgroundImage(this);">< $1000</option>
</select>
</form>
CSS代碼:
html{
background: url('http://upload.wikimedia.org/wikipedia/commons/8/8a/1000_USD_note%3B_series_of_1934%3B_obverse.jpg') no-repeat center center fixed;
/*-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;*/
background-size: cover;
min-width:1300px; /*keep the same width when resizing*/
}
JAVASCRIPT:
var bg = document.getElementsByTagName("html").style.backgroundColor = "white";
// function to dynamically change the background image when user hovers over an option in select menu
function changeBackgroundImage(bg)
{
bg.document.getElementsByTagName("html").style.backgroundImage = "url('http://upload.wikimedia.org/wikipedia/commons/b/be/500_USD_note%3B_series_of_1934%3B_obverse.jpg')";
}
我得到一個錯誤'類型錯誤:bg.document是\t \t \t \t undefined'' bg.document.getElementsByTagName(「html」)[0] .style.backgroundImage =「url('http://upload.wikimedia。組織/維基百科/公地/ B /是/ 500_USD_note%3B_series_of_1934%3B_obverse.jpg')「;' – TheAmazingKnight