javascript 未捕获的类型错误:无法读取 null 的属性“播放、源代码或点击”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26704188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught TypeError: Cannot read property 'play,src, or click' of null
提问by Drake Wallington
Ok so Chrome is give me this error Uncaught TypeError: Cannot read property 'play' of nullthis keep on popping up if switch it to click or focus function. So in my simple HTML code I have
好的,所以 Chrome 给了我这个错误Uncaught TypeError: Cannot read property 'play' of null如果将其切换到单击或聚焦功能,它会继续弹出。所以在我的简单 HTML 代码中,我有
<audio id="f1Menu" controls>
<source src="../sounds/menu/menu_f1_help.mp3" type="audio/mp3">
<source src="../sounds/menu/menu_f1_help.ogg" type="audio/ogg">
</audio>
<audio id="f2Menu" controls>
<source src="../sounds/menu/menu_f2_email.mp3" type="audio/mp3">
<source src="../sounds/menu/menu_f2_email.ogg" type="audio/ogg">
</audio>
<audio id="f3Menu" controls>
<source src="../sounds/menu/menu_f3_password.mp3" type="audio/mp3">
<source src="../sounds/menu/menu_f3_password.ogg" type="audio/ogg">
</audio>
<audio id="f4Menu" controls>
<source src="../sounds/menu/menu_f4_enter.mp3" type="audio/mp3">
<source src="../sounds/menu/menu_f4_enter.ogg" type="audio/ogg">
</audio>
and the the Javascript script which is load on the same page
以及加载在同一页面上的 Javascript 脚本
//this javascript which is attach to html page.
//这个附加到html页面的javascript。
AudioMode().playAudio("f1",2000);
function AudioMode()
{
this.playAudio = function(key,delay)
{
alert(key + "Menu"); //this display on screen with the the id combo im looking for
//however the document.getElementById is not find it???
document.getElementById(key + "Menu").play();
}
}
If need more info please let me know. I place min code which giving me error the function work just not in it.
如果需要更多信息,请告诉我。我放置了最小代码,这给了我错误的函数工作只是不在其中。
采纳答案by nicael
Your element hasn't loaded yet. You should wait until it loads.
您的元素尚未加载。你应该等到它加载。
Replace
代替
AudioMode().playAudio("f1",2000);
with
和
window.onload=function(){
AudioMode().playAudio("f1",2000);
}