Add chars on line break
Hi I have list (ul) and in it some "li".
With Javascript (jQuery) how do I detect when line breaks and add some chars on beginning.
So for example:
I have a very looooong text 开发者_JS百科in 200px width li.
Result is:
I have a very looooong text
in 200px width li.
And I want to add " + " on the beginning of new line => result:
I have a very looooong text
+ in 200px width li.
thx
How about something in pure CSS? Demo - http://jsfiddle.net/FDD7n/
Working for me in Chrome 11. This might require some tweaking depending on your font size :)
Tough one, here's an idea: Let's make an extra div that is positioned the same place as the li with the same css properties. Give the li a padding-left of 15px. Fill the new div with +'s for each line the li is higher than 1 line.
var plusses = $('li').height() / $('li').css('line-height').slice(0,-2); //number of lines
var i = 1;
var sLines = '<br />'; // the first line doesn't need a +
while(i < plusses) {
sLines+='+<br />'; // every other line does
i++;
}
$('#newdiv').html(sLines);
Then all you need to do is css'ing the bunch!
If the browser decides to place text on a separate line because the parent's width isn't enough, there is probably no way to detect that. If you yourself are creating the line breaks in your HTML, you could look for "\n" and replace them with "\n+". You could, for example, calculate the text width ( Calculating text width ) and check it against the parent width, although it might not be so easy guessing where the browser decides to break the line.
The only way I can think of is (assuming the browser will wrap break on a white-space) wrap all words in a span and then programatically loop through the sibling keeping track of the y pos. If there's a significant change in the y pos, you've probably wrapped... So you add the "+" and keep iterating.
Bear in mind that adding a " + " will probably affect where the remainder of the string is going to wrap as well and can cause an additional line break
精彩评论