2015-10-26 75 views
-1

我有一個div內的視頻元素。我只想將視頻高度設置爲整個窗口。我發誓它正在工作,但在某些時候我打破了,並不明白爲什麼...Jquery:無法選擇視頻標籤

jQuery選擇器的長度= 0,也許是因爲視頻加載後,jQuery作出選擇,但直到2天前正在

var $current_height = $(window).height(); 
 
var $videohome = $('video'); 
 
$videohome.height($current_height);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> 
 
<div class="video full_height center"><a href="page"> 
 
     <video class="full_height" poster="sites/all/themes/my_theme/img/cover_video1.jpg" onmouseover="this.play()" onmouseout="this.load()" id="video1" loop="true"> 
 
     <source src="sites/default/files/videos/video1.mp4" type="video/mp4"></source> 
 
     <source src="sites/default/files/videos/video1.ogg" type="video/ogg"></source> 
 
     </video></a> 
 
</div>

筆記:

  • 在頁面其他影片,但我猜例如僅1是確定
  • 這個代碼工作與其他元素的種類在頁面(div的,例如)
  • 使用:Drupal的7.41和jQuery 1.4.4

在此先感謝

+0

'$ videohome.height($ current_height);'叫什麼? jQuery加載之前? – guest271314

+0

不,即使在同一行中,如果我選擇了其他DOM元素而不是視頻,它仍在工作 – HEDMON

+0

嘗試使用'.ready()'。查看文章 – guest271314

回答

0

嘗試包裝js$(document).ready(function() {}),看到.ready()

$(document).ready(function() { 
 
    var $current_height = $(window).height(); 
 
    var $videohome = $("video"); 
 
    $videohome.height($current_height); 
 
    $videohome[0].load(); 
 
    $videohome[0].play(); 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"> 
 
</script> 
 
<div class="video full_height center"> 
 
    <video class="full_height" id="video1" controls loop="true"> 
 
    <source id="currentVID" src="http://html5multimedia.com/code/ch9/media/elephants-dream-medium.mp4" type="video/mp4"> 
 
    </video> 
 
</div>

+0

由於Drupal 7的最佳實踐,我避免了$(document).ready() 在這裏你可以看到文件:http://dev.hedmon.com/ronzak/sites/all/themes/ronzak_theme/js/ronzak。 js 這裏的問題http://dev.hedmon.com/ronzak/ – HEDMON

+0

啊!最重要的是:非常感謝您的幫助! ;) – HEDMON