Javascript - getting the last word in a string
Here is an example:
<div><span style="font-size:15px;">some text here which is </span>
<span style="color:red;">split automatically by the browser.</span></div>
which the browser displays as (with styles, of course):
some text here which is split
automatically by the browser.
I am creating a javascript application that needs to understand the last word on each line ( splitin this case). I am currently using the CSS property white-spaceto handle overflow text. Therefore, the browser will not let me know where it breaks the line. So, what would be a good function for understanding the last word on each line?
EDIT
I use pure-javascript, so jQuery will not be an option, but the property white-spacewill be applied to div. Therefore, actually think divas a means of visualizing the line.
, , . :
HTML:
<div id="flop" class="foobar">Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur.</div>
<br>
<button value="Go" onclick="foo();">Go</button>
Script:
function foo(){
var d = document.getElementById('flop');
var t = d.innerHTML;
var w = t.split(' ');
d.innerHTML = w[0];
var height = d.clientHeight;
for(var i = 1; i < w.length; i++){
d.innerHTML = d.innerHTML + ' ' + w[i];
if(d.clientHeight > height){
height = d.clientHeight;
console.log(w[i-1]);
}
}
}
, . . , .
, Yi Jiang , SO.