jQuery - 使用循环和延迟使文本变粗

时间:2018-04-16 17:29:11

标签: javascript jquery loops

我正在尝试在jQuery中创建类似的东西:

var x = 0;

function start(){
    setInterval(function () {return changeText(x++);}, 2000);
}

function changeText(index){
    var words = getTextFromDiv().split("/");
    var result = "";

    for(var i = 0; i < words.length; i++){
        if(i == index)
            words[i] = "<b>" + words[i] + "</b>";

        result += words[i];

        if(i != words.length - 1)
            result += "/";
    }
    document.getElementById("divId").innerHTML = result;
    if(x == words.length){
        x = 0;
    }
}

//get text from element without HTML
function getTextFromDiv() {
    var element = document.getElementById('divId');
    return element.innerText || element.textContent;
}

start();
<div id="divId">
   aaa / bbb / ccc / eee
</div>

我尝试使用每个循环和延迟。但它没有用。你知道怎么做吗?

0 个答案:

没有答案