我有代码
function splitString(stringToSplit, separator) {
var str= "It’s that time of year when you clean out your closets, dust off shelves, and spruce up your floors. Once you’ve taken care of the dust and dirt, what about some digital cleaning? Going through all your files and computers may seem like a daunting task, but we found ways to make the process fairly painless."
var myArray = stringToSplit.split(" ")
for (var i=0; i < myArray.length; i++)
{
}
var yourSpan = document.createElement('span');
yourSpan.innerHTML = "";
var yourDiv = document.getElementById('divId');
yourDiv.appendChild(yourSpan);
yourSpan.onmouseover = function () {
    alert("On MouseOver");
}对于html,我有
<div id="transcriptText"> It’s that time of year when you clean out your
closets, dust off shelves, and spruce up your floors. Once you’ve taken
care of the dust and dirt, what about some digital cleaning? Going
through all your files and computers may seem like a daunting task, but
we found ways to make the process fairly painless.</div>
<br>
<div id="divideTranscript" class="button"> Transform the
Transcript!  </div>T我需要在循环中创建跨度,我需要创建一个跨度,将class和id属性(对于每个单词都不同-它包括数组的索引(i))添加到span元素,在跨度内添加单词,向跨度添加mouseover/mouseout事件侦听器,将跨度添加到原始div。有人能帮我吗?这不是很多,我只是不确定我应该如何做这些最后的改变。
发布于 2014-05-01 11:53:40
var yourDiv = document.getElementById('divId');
for (var i = 0; i < myArray.length; i++) {
    var yourSpan = document.createElement('span');
    yourSpan.innerText = myArray[i];
    yourSpan.id = "yourspan_" + i;
    yourSpan.className = "yourclass";
    yourDiv.appendChild(yourSpan);
    yourSpan.onmouseover = function () {
        alert("On MouseOver");
    }
}发布于 2020-12-23 07:36:25
for (let i = 0; i < raceTraits.ability.length;i++){
        document.getElementById("Race").innerHTML +=  "<span id =" + i + ">" + "</span>";
}
/*you could use the value in the for loop and plugin "i" to make this easier*/https://stackoverflow.com/questions/23401712
复制相似问题