2014-05-01 30 views
0

我的代碼跨度環的Javascript

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">&nbsp;Transform the 
Transcript!&nbsp; </div>T 

我需要跨距創建的循環中, 我需要創建一個跨度,添加類和id屬性(每個單詞都有所不同 - 它包括數組(i)的索引))添加到span元素中,在span中添加單詞,向span添加mouseover/mouseout事件偵聽器,將span添加到原始DIV。 任何人都可以幫助我嗎?我只是不確定我應該如何去做這些最後的改變。

回答

1
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"); 
    } 
} 
+0

這將非常感謝你 – user3591618