简体   繁体   English

用html标签替换段落中的每个单词

[英]Replacing every word in a paragraph with html tags

I want replace every word in a paragraph with some html tags, for example, consider this text : 我想用一些html标记替换段落中的每个单词,例如,考虑以下文本:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Lorem ipsum dolor坐下来,管教着迷。

and the result after replacing the words with html tags, it should be like : 和用html标签替换单词后的结果,应该像这样:

<span class="word">Lorem</span> <span class="word">ipsum/span> <span class="word">dolor</span> <span class="word">sit</span> <span class="word">amet</span>, <span class="word">consectetuer</span> <span class="word">adipiscing</span> <span class="word">elit</span>.

Need some guidance on how to achieve this. 需要一些有关如何实现这一目标的指导。 Thanks. 谢谢。

"Lorem ipsum dolor sit amet, consectetuer adipiscing elit".split(" ").map(function (word) {
    return '<span class="word">' +word +'</span>';
}).join("");
       var text = yourtext.split(' ');

       for( var i = 0, len = text.length; i < len; i++ ) {
           text[i] = '<span class="word">' + text[i] + '</span>';
       }


  });

In text you will get words with span tags 在文本中,您将获得带有跨度标签的单词

See more at: http://www.grasmash.com/article/jquery-wrap-each-word-element-unique-span-tag#sthash.y0DkyzDW.dpuf 详情请参见: http : //www.grasmash.com/article/jquery-wrap-each-word-element-unique-span-tag#sthash.y0DkyzDW.dpuf

you van do this: 您可以这样做:

<script>

    var x="Lorem ipsum dolor sit amet, consectetuer adipiscing elit".split(" ").map(function (word) {
        return '<span class="word">' +word +'</span>';
    }).join(" ");

    document.getElementById('htmlForm').innerHTML=x;


</script>

<p id="htmlForm"></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM