即使使用jQuery DOM就绪事件,也无法调用null的方法addEventListener()
Cannot call method addEventListener() of null - even using jQuery DOM ready event
我使用jQuery for/with javascript来尝试修改.htm文件中div的内部HTML代码。我使用这里推荐的jQueryDOM就绪事件(http://stackoverflow.com/questions/9856140/javascript-uncaught-typeerror-cannot-call-method-addeventlistener-of-null)为了避免在div准备好之前运行脚本,但这并不重要;我总是犯那个错误。
我必须说,我刚开始使用Javascript只有几天了,距离我上次使用HTML已经很长时间了,所以请不要丢弃任何典型的错误,因为很可能就是这样。
以下是我正在使用的文件,托管在Skydrive中:
AMPlayer.htm
播放列表.js
可以猜到,我正在使用videoJS作为视频播放器。videojs的CSS和JS文件可以在线引用(http://videojs.com/#section5)或者离线,如AMPlayer.htm中所示,但这只是环境信息,因为它看起来更像是javascript的问题,而不是videojs的问题。
编辑:我认为是文件的重要部分:
AMPlayer.htm(我认为问题就在这里,所以我正在粘贴整个文件):
<!DOCTYPE html>
<html>
<head>
<link href="video-js.css" rel="stylesheet" type="text/css">
<script src="video-js.js"></script>
<script src="jquery-1.7.2.min.js"></script>
<script src="playlister.js" type="text/javascript"></script>
<title>AMPlayer</title>
</head>
<body>
<div id="the-video" class="video-js-box">
<span> </span>
</div>
</body>
</html>
playlister.js:
function postVideo()
{
getNextVideo();
document.write("NEXT VIDEO: "+videoNames[iNewVideoId]+"<br>");
var video1 = '<video id="AMPlayer" class="video-js vjs-default-skin" preload="auto" width="650" height="365" data-setup="{}" autoplay> <source src="videos/AVANCE/vid.mp4" type="video/mp4" /><source src="videos/AVANCE/vid.webm" type="video/webm" /></video>';
//document.write(video1); This writes the text so the video is played, but it drives me to an undesired behaviour
document.write("BEFORE THE .SETUP<br>");
//$("#the-video").html(video1); This doesn't work
//document.getElementById("the-video").innerHTML=video1; This does neither work
//window.VideoJS.setup($'video1'));
document.write("AFTER THE .SETUP<br>");
document.getElementById("AMPlayer").addEventListener('ended', postVideo, true);
//$(".video-js")[0].player.play();
document.write("AFTER THE .PLAY<br>");
//postVideo();
}
任何提供的帮助肯定都是非常有用的。提前谢谢。
您正在尝试获取id为AMPlayer
的元素,但没有。
您有一个包含HTML的字符串来创建一个,但它还没有添加到DOM中。
相关文章:
- 从JavaScript调用JAX-WSWeb服务时参数为Null
- 为什么我发布的数据在这个AJAX调用的Controller目标中为null
- 背景:'userId'在SignUp API调用后为null
- 在映射空闲时调用maps.getBounds()为null
- 未捕获的类型错误:无法调用方法'shift'Chrome中的Facebook JS库为null
- 谷歌脚本在第二次调用Gmail后返回null
- moment:Array.prototype.some在null或未定义时被调用
- 无法调用方法'appendChild'为null
- Ajax调用后控制器中的参数为Null
- 为什么我的ajax调用返回null
- 当与全局标志一起使用时,Javascript Regex在后续调用中返回null
- JS:.apply(null,arguments)——为什么我的函数被调用一次,而console.log被调用多次
- window.opener调用函数给了我一个文档.getElementById(..)为null
- 即使使用jQuery DOM就绪事件,也无法调用null的方法addEventListener()
- 获取文本框输入的值:未捕获的类型错误:无法调用方法'val'为null
- 为什么我总是得到“无法调用 null 的方法'addEventListener'”
- 收到错误消息:“未捕获的类型错误:无法读取 null 的属性'焦点'”时尝试调用 focus() 到元素
- 解析云代码:“在关系上调用 .query() 时,”无法调用 null 的方法'_toPointer'”
- activejs:数据绑定文本区域导致无法用null调用removeChild
- 在CRUD系统中对null调用成员函数prepare()