0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

文字列をspanタグで囲む

Posted at
<div class="items">
    <h1 class="item">titletitle</h1>
    <p class="item">texttexttexttexttexttexttexttexttext</p>
</div>

(function(){
const items = document.querySelectorAll('.item');
items.forEach(target => {
let newText = '';
const text = target.textContent;
const result = text.split('');
for(let i = 0; i < result.length; i++){
newText += '' + result[i] + '';
}
target.innerHTML = newText
});
}());

変数の用途
・jstext→要素の取得
・target[引数]→取り出した要素の格納
・newText要素を入れる空の変数
・text取り出したtarget要素を取得
・result取得した文字を格納

for文内の記述
・resultで取り出した文字列を繰り返しspanタグで囲っていく。
囲った文字列をnewTextに格納する。

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?