使用javascript增加类

我怎样才能增加特定的课程?

我正在使用克隆方法,我已经成功克隆了Id并增加了它.但我想在我正在增加的Div中增加我的课程.

我的课程增量:.emptybox

我的js:

//duplicate Play Timer
    var i = 0;
    var original = document.getElementById('Play_Start');
    var count = 1;

    function duplicatePlay() {
        if (count < 20) {
            var clone = original.cloneNode(true); // "deep" clone
            clone.id = "Play_Start" + ++i; // there can only be one element with an ID
            original.parentNode.appendChild(clone);
            $(clone).effect( "highlight", {color:"#FEFFB9"}, 3000 );
            count++;
        } else {
        }

    }

最佳答案 你可以做这样的事情

var i = 0;
var original = document.getElementById('Play_Start');
var count = 1;

function duplicatePlay() {
  if (count < 20) {
    var clone = original.cloneNode(true); // "deep" clone
    clone.id += ++i; // there can only be one element with an ID   

    clone.querySelector('.emptybox').className = 'emptybox' + i;

    original.parentNode.appendChild(clone);
    $(clone).effect("highlight", {
      color: "#FEFFB9"
    }, 3000);
    count++;
  } else {}

}

duplicatePlay();
<div id="Play_Start" class="abc cde">hi <span class="emptybox">user</span>
</div>
点赞