Use Javascript to get the Sentence of a Clicked Word

后端 未结 5 2160
甜味超标
甜味超标 2021-02-05 21:37

This is a problem I\'m running into and I\'m not quite sure how to approach it.

Say I have a paragraph:

\"This is a test paragraph. I love cats. Please a         


        
5条回答
  •  半阙折子戏
    2021-02-05 22:20

    Not sure how to get the complete sentense. but you can try this to get word by word if you split each word by spaces.

         
    This is a test paragraph. I love cats. Please apply here
    function splitToSpans(element){ if($(element).children().length) return; var arr = new Array(); $($(element).text().split(' ')).each(function(){ arr.push($(''+this+' ')); }); $(element).text(''); $(arr).each(function(){$(element).append(this);}); }

提交回复
热议问题