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

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

I

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 07:30

    It's gonna be a little more complicated than that. You're gonna have to find out all the words and re-append them to your element, wrapped in a span.

    var words = $("p").text().split(" ");
    $("p").empty();
    $.each(words, function(i, v) {
        $("p").append($("").text(v));
    });
    

    Live example

提交回复
热议问题