js 实现省略号在左边显示

<!DOCTYPE html>
<html>
<head>
<style>
#container {
200px;
border: 1px solid blue;
}
#container div {
100%;
overflow: hidden;
white-space: nowrap;
}
</style>
<script>
function trimRows() {
var rows = document.getElementById('container').childNodes;
for (var i=0, row; row = rows[i]; i++)
{
if (row.scrollWidth > row.offsetWidth) {
var textNode = row.firstChild;
var value = '...' + textNode.nodeValue;
do {
value = '...' + value.substr(4);
textNode.nodeValue = value;
} while (row.scrollWidth > row.offsetWidth); }
}
}
</script>
</head>
<body onload='trimRows();'>
<div id="container" >
<div>first > second > third</div>
<div>second > third > fourth > fifth > sixth</div>
<div>fifth > sixth > seventh > eighth > ninth</div>​
</div>





<style>
p {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
170px;
border:1px solid #999;
direction:rtl;
text-align:left;
}
</style>
<p>first &gt; second &gt; third<br />
second &gt; third &gt; fourth &gt; fifth &gt; sixth<br />
fifth &lt; sixth &lt; seventh &lt; eighth &lt; ninth</p>

<script>
var text = $( 'p' ).text(),
split = text.split( ' ' ),
finalStr = '';
for( i in split ){
finalStr = finalStr.length > 0 ? finalStr + '<br />' : finalStr;
var match = /(w+s?(<|>)?s?){3}$/.exec( split[i] );
finalStr = finalStr + ( split[i].length > match[0].length ? '...' : '' ) + match[0]; } $( 'p' ).empty().html( finalStr );
</script>






</body>
</html>

原文地址:https://www.cnblogs.com/lhy2013/p/3648414.html