简体   繁体   English

切换类Jquery

[英]toggling class Jquery

I have created a play and pause button for my video, I have trying to switch class when the button is pressed, Ie when you click on the pause button the class switches to and vice versa. 我为我的视频创建了一个播放和暂停按钮,我试图在按下按钮时切换班级,即当您单击暂停按钮时,班级切换到班级,反之亦然。

Below is a snippet of my code of and the link to the code is also here 以下是我的代码片段,并且代码链接也位于此处

$('#play-pause-btn').click(function() {
    $('#video-wall__content').get(0).paused ? 
        $('#video-wall__content').get(0).play() : $('#video-wall__content').get(0).pause();
});

does anyone know how this can be done? 有谁知道该怎么做?

Check out removeClass() , and addClass() ...Well they do what they are named after. 看看removeClass()addClass() 。。。

http://api.jquery.com/removeclass/ http://api.jquery.com/removeclass/

http://api.jquery.com/addclass/ http://api.jquery.com/addclass/

If you want to change the class. 如果要更改班级。 Try .attr() 试试.attr()

$("#td_id").attr('class', 'newClass');

Or .toggleClass() : .toggleClass()

$("#td_id").toggleClass('change_me newClass');

You can remove/add the classes on click using removeClass and addClass . 您可以使用removeClassaddClass在单击时删除/添加类。

$('#play-pause-btn').click(function(){
    if ($('#video-wall__content').get(0).paused) {
        $('#video-wall__content').get(0).play();
        // now playing
        $(this).removeClass("paused").addClass("playing");
    }
    else {
        $('#video-wall__content').get(0).pause();
        // now paused
        $(this).removeClass("playing").addClass("paused");
    }
 });

Addding the class when the page is opened 打开页面时添加类

If you have access to the HTML, just add it to your tag: 如果您有权访问HTML,只需将其添加到代码中:

<div id="play-pause-btn" class="playing> [...] </div>

If you can use only JavaScript: 如果只能使用JavaScript:

$(document).ready(function() {
    $("#play-pause-btn").addClass("playing");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM