如何修复';TypeError未定义';Javascript中函数的消息?
当用户将鼠标悬停在选定的选项上时,我尝试操纵背景图像。我在firebug中测试了我的代码并加载了页面。我立刻得到: TypeError:document.getElementsByTagName(…)。样式未定义 对于悬停,我得到: 类型错误:bg.document未定义 如何修复此错误 HTML代码:如何修复';TypeError未定义';Javascript中函数的消息?,javascript,html,dom-events,background-image,onmouseover,Javascript,Html,Dom Events,Background Image,Onmouseover,当用户将鼠标悬停在选定的选项上时,我尝试操纵背景图像。我在firebug中测试了我的代码并加载了页面。我立刻得到: TypeError:document.getElementsByTagName(…)。样式未定义 对于悬停,我得到: 类型错误:bg.document未定义 如何修复此错误 HTML代码: <form> <!--show a large print of green font color and size money--> Select yo
<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>
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*/
}
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')";
}
JAVASCRIPT:
<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>
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*/
}
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')";
}
getElementsByTagName()
返回匹配元素的数组,
因此,您应该在您的案例中使用index0
应该是
document.getElementsByTagName("html")[0].style
而不是
document.getElementsByTagName("html").style
getElementsByTagName()
返回匹配元素的数组,
因此,您应该在您的案例中使用index0
应该是
document.getElementsByTagName("html")[0].style
而不是
document.getElementsByTagName("html").style
顾名思义:
document.getElementsByTagName
返回标记的集合而不是单个节点,我之所以说是集合,是因为它实际上不返回数组,而是返回类似数组的对象
如果只需要获取html节点,只需执行以下操作:
document.lastChild.style.backgroundColor = "white";
而且
document.documentElement.style.backgroundColor = "white";
但如果需要使用类似的查找器函数查找,则应改为:
document.getElementsByTagName("html")[0].style.backgroundColor = "white";
还有这个:
document.querySelector("html").style.backgroundColor = "white";
顾名思义:
document.getElementsByTagName
返回标记的集合而不是单个节点,我之所以说是集合,是因为它实际上不返回数组,而是返回类似数组的对象
如果只需要获取html节点,只需执行以下操作:
document.lastChild.style.backgroundColor = "white";
而且
document.documentElement.style.backgroundColor = "white";
但如果需要使用类似的查找器函数查找,则应改为:
document.getElementsByTagName("html")[0].style.backgroundColor = "white";
还有这个:
document.querySelector("html").style.backgroundColor = "white";
我收到一个错误
TypeError:bg.document在bg.document.getElementsByTagName(“html”)[0]上未定义http://upload.wikimedia.org/wikipedia/commons/b/be/500_USD_note%3B_series_of_1934%3B_obverse.jpg')";
我在bg.document.getElementsByTagName(“html”)[0].style.backgroundImage=“url”(“html”)上遇到一个错误TypeError:bg.document未定义http://upload.wikimedia.org/wikipedia/commons/b/be/500_USD_note%3B_series_of_1934%3B_obverse.jpg')";
我在bg.document.getElementsByTagName(“html”)[0].style.backgroundImage=“url”(“html”)上遇到一个错误TypeError:bg.document未定义http://upload.wikimedia.org/wikipedia/commons/b/be/500_USD_note%3B_series_of_1934%3B_obverse.jpg')";
我在bg.document.getElementsByTagName(“html”)[0].style.backgroundImage=“url”(“html”)上遇到一个错误TypeError:bg.document未定义http://upload.wikimedia.org/wikipedia/commons/b/be/500_USD_note%3B_series_of_1934%3B_obverse.jpg')";代码>