Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何修复';TypeError未定义';Javascript中函数的消息?_Javascript_Html_Dom Events_Background Image_Onmouseover - Fatal编程技术网

如何修复';TypeError未定义';Javascript中函数的消息?

如何修复';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

当用户将鼠标悬停在选定的选项上时,我尝试操纵背景图像。我在firebug中测试了我的代码并加载了页面。我立刻得到:

TypeError:document.getElementsByTagName(…)。样式未定义

对于悬停,我得到:

类型错误:bg.document未定义

如何修复此错误

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);">&lt; $500</option>
        <option value="thousandDollars" onMouseOver="changeBackgroundImage(this);">&lt; $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);">&lt; $500</option>
        <option value="thousandDollars" onMouseOver="changeBackgroundImage(this);">&lt; $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()
返回匹配元素的数组, 因此,您应该在您的案例中使用index
0

应该是

document.getElementsByTagName("html")[0].style
而不是

document.getElementsByTagName("html").style
getElementsByTagName()
返回匹配元素的数组, 因此,您应该在您的案例中使用index
0

应该是

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')";