More than 5 years have passed since last update.

JavaScript 文字列を一文字ずつspanで囲む

Last updated at Posted at 2015-08-30

<div id="box">Hello World!</div>

var textbox = document.querySelector('#box');
var text = textbox.textContent;

textbox.innerHTML = null;

text.split('').forEach(function (c) {
  textbox.innerHTML += '<span>'+c+'</span>';

text.split('') で 文字列を一文字ずつ分割して配列化する。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up