想要在纯JavaScript中添加/删除类

时间:2016-10-19 20:00:32

标签: javascript

我想在纯JavaScript中重写下面的jQuery代码以节省加载时间。

Here is the link to the demo page

var playstate = function (){
    $(".magicwand").addClass("logo-effect");
setTimeout(function () {
    $(".magicwand").removeClass("logo-effect");
        }, 6000);
}
playstate();
setInterval(playstate, 6100);

1 个答案:

答案 0 :(得分:0)

试试这个:我已经将你的jquery代码转换为纯javascript。



var playstate = function() {
  var element = document.getElementsByClassName("magicwand");
  element[0].classList.add("logo-effect");
  setInterval(function() {
    var element = document.getElementsByClassName("magicwand");
    element[0].classList.remove("logo-effect");
  }, 6000);
}
playstate();
setInterval(playstate, 6100);

<div class="magicwand">test</div>
&#13;
&#13;
&#13;