How to wrap each word of an element in a span tag?

前端 未结 8 1773
忘掉有多难
忘掉有多难 2020-12-01 07:05
$(\"div.date\")
    .contents()
    .filter(
        function(){
            return this.nodeType != 1; 
        })
    .wrap(\"\");

I

8条回答
  •  [愿得一人]
    2020-12-01 07:44

    Just building on Xeon06 excellent answer.

    I had to do this for a multiple of the same element on the same page.

        $('.element').each(function(){
            var words = $(this).text().split(" ");
            var total = words.length;
            $(this).empty();
            for (index = 0; index < total; index ++){
                $(this).append($(" ").text(words[index]));
            }
        })
    

提交回复
热议问题