2017-08-02 54 views
1

我想爲每個使用Jquery的點擊播放創建一個觸發器,但它只能用於我找到的方法,需要將標籤數據組應用到HTML中。我創建通過視覺作曲家(WordPress的),這觸發我只能申請一個自定義類和ID以每個視頻,所以我不能用目前的方法Onclick爲多個視頻播放Jquery觸發器

Codepen https://codepen.io/danielvianna/pen/jLBdYo

HTML

<div class="video_container"> 
<video class="video-ctrl" loop="" playsinline="" autoplay> 
    <source src="http://sandbox.cast-soft.com/wp-content/uploads/2017/04/seating_handbrake.mp4" type="video/mp4"> 
    </video> 
</div> 

<div> 
    <span class="truss_wizard" data-group="play">clickme1</span> 
    <video poster="http://sandbox.cast-soft.com/wp-content/uploads/2017/04/truss_wizard_poster.jpg"> 
    <source src="http://sandbox.cast-soft.com/wp-content/uploads/2017/04/truss_handbrake.mp4" type="video/mp4"> 
    </video> 
</div> 

<div> 
    <span class="projection_wizard" data-group="play">clickme2</span> 
    <video poster="http://sandbox.cast-soft.com/wp-content/uploads/2017/04/projection_poster.jpg"> 
    <source src="http://sandbox.cast-soft.com/wp-content/uploads/2017/04/projection_handbrake.mp4"> 
    </video> 
</div> 

<div> 
    <span class="pipe_drape_wizard" data-group="play">clickme3</span> 
    <video poster="http://sandbox.cast-soft.com/wp-content/uploads/2017/04/Pipe-and-Drape_poster.jpg"> 
    <source src="http://sandbox.cast-soft.com/wp-content/uploads/2017/04/pipe_drape_handbrake.mp4" type="video/mp4"> 
    </video> 
</div> 

jQuery代碼

//making it more dynamic with a data attribute 
// <span class="square" data-group="play2">clickme2</span> 

jQuery(function($) { 
    //change the elements so they have a class and you can find multiple 
    var $players1 = $('.truss_wizard'); 
    var $players2 = $('.projection_wizard'); 
    var $players3 = $('.pipe_drape_wizard'); 

    $players1.click(function() { 
    //find all the playbutton elements 
    $players1.filter('[data-group="'+ $(this).data('group') +'"]') 
    //get all their parent divs 
    .closest('div') 
    //find their nested videos 
    .find('video') 
    //play each of them 
    .each(function(){ this.play(); 
        }); 
    $players2.click(function() { 
    //find all the playbutton elements 
    $players2.filter('[data-group="'+ $(this).data('group') +'"]') 
    //get all their parent divs 
    .closest('div') 
    //find their nested videos 
    .find('video') 
    //play each of them 
    .each(function(){ this.play(); 
         }); 
    $players3.click(function() { 
    //find all the playbutton elements 
    $players3.filter('[data-group="'+ $(this).data('group') +'"]') 
    //get all their parent divs 
    .closest('div') 
    //find their nested videos 
    .find('video') 
    //play each of them 
    .each(function(){ this.play(); 
         }); 
     }); 
    }); 
    }); 
});//function closes 

CSS

.truss_wizard,.projection_wizard,.pipe_drape_wizard{ 
     height: 50px; 
     width: 200px; 
     display: block; 
     background-color: #7ac810; 
    font-size: 15; 
} 

span { 
    font-color: yellow; 
    font-size: 30px; 
} 

.video { 
margin-bottom: 100px; 
} 

注:我使用的是包裝,因爲我在WordPress與視覺作曲家建立了一個頁面中插入該

+0

您已經重複'play2'ID。 ID必須是唯一的。一個id的選擇器只能找到一個元素。改變它有一個類,而它會找到多個然後。 – Taplar

回答

1

jQuery(function($) { 
 
    //change the elements so they have a class and you can find multiple 
 
    $(".play2").click(function() { 
 
    //find all the play2 elements 
 
    $(".play2") 
 
     //get all their parent divs 
 
     .closest('div') 
 
     //find their nested videos 
 
     .find('video') 
 
     //play each of them 
 
     .each(function(){ this.play(); }); 
 
    }); 
 
}); 
 

 
//making it more dynamic with a data attribute 
 
// <span class="square" data-group="play2">clickme2</span> 
 

 
jQuery(function($) { 
 
    //change the elements so they have a class and you can find multiple 
 
    var $players = $('.square'); 
 
    
 
    $players.click(function() { 
 
    //find all the play2 elements 
 
    $players.filter('[data-group="'+ $(this).data('group') +'"]') 
 
     //get all their parent divs 
 
     .closest('div') 
 
     //find their nested videos 
 
     .find('video') 
 
     //play each of them 
 
     .each(function(){ this.play(); }); 
 
    }); 
 
});

+0

我做了這些改變,但似乎所有的視頻在同一時間播放,我想每次觸發一個,但它是各自的按鈕。注意:該按鈕與視頻不在同一個div中,我試圖從作爲選項卡一部分的跨度代碼觸發視頻。我用你的代碼創建了新的代碼:https://codepen.io/danielvianna/pen/mMOJbz –

+0

@DanielVianna你的所有數據組在codepen中都是一樣的,其值爲'play'。 – Taplar

+0

我能夠使其工作,但我仍然有問題。我意識到我無法添加數據組。我通過視覺作曲家黑客攻擊。我只能將ID和Class添加到每個選項卡,但我無法添加數據組。有沒有解決這個問題的方法?注意:第一個視頻不應該被觸發,而是自動自動播放,所以我把它放在了該功能之外。這是我可以訪問的屏幕截圖:http://i.imgur.com/qWIdZ0j.jpg –