2016-03-06 152 views
-2

我想要一個播放視頻的功能。我有以下代碼,但它不起作用。.play(function(){}); jquery不能正常工作

$(".jw-video")[0].play(function(){ 
    var link = $(".jw-video").attr("src"); 
    console.log(link); 
}); 

請幫忙。

+0

控制檯中的任何錯誤?你爲什麼認爲它會起作用? –

+0

也許試試看: '$(「。jw-video」)[0] .live('playing',function(){varnail link = $(「。jw-video」)。attr(「src」) ; console.log(link); });' – sscotti

+2

@sscotti'.live'已過時,已被刪除一段時間。加'[0]'表示它可能不是jQuery對象。 –

回答

0

做了一點研究。這似乎工作,雖然可能比混合和匹配直的JS和jQuery更好的方法,它可以清理一下,但它似乎工作。這是一個非常有用的鏈接:

W3 Org Video Media Events

JS/jQuery的

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
<script type="text/javascript"> 


$(function() { 
videoElement = $(".jw-video")[0]; 
videoElement.addEventListener("playing", playHandler, false); 
function playHandler(e) { 
    alert("playing"); 
    var link = $(".jw-video")[0].currentSrc; 
    console.log(link); 
} 
}); 
</script> 

HTML

<video class="jw-video" width="320" height="240" controls> 
<source id="mp4" src="test.mp4" type="video/mp4"> 
Your browser does not support the video tag. 
</video> 
+0

不知道爲什麼$(「。jw-video」)[0] .controller返回null?不在代碼中,但我想看看它是如何工作的。 – sscotti