When you add a value to a string in JS, it automatically casts it to a string as well. The default string representation of an array is just the elements separated by commas, which is why you're getting what you're getting. In order to make each one different, you might want to map the array, which has syntax like this:
[1, 2, 3].map((n) => 2*n) // gives [2, 4, 6]
If you map the array, you could format it to add the HTML tags around each word, for example like:
text_array.map((word) => "<span>" + word + "</span>").join(" ")
To make each of them have a separate mouse over event, you could just put that straight into the element you create, like this:
text_array.map((word, i) =>
"<span onmouseover='console.log(" + i + ")'>" + word + "</span>"
).join(" ")
Alternatively, you might want to create elements in a more object-oriented way, using document.createElement
. Then you can add listeners to them using their addEventListener
method, and your code will be a lot easier to read. For tooltips in particular, you're better off just following a tutorial that is focused explicitly on how to make them. Javascript gives you a very clean API to modify html and css, so if you can hardcode it, it's a small step to be able to programmatically do it.
As a side note, the code in your question shouldn't work, because you never defined glossTxt inside the loop. As another side note, the capitalization scheme in js is lower camel case, so display_list should really be displayList.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…