var divs = document.querySelectorAll('div');
var div = divs[0]
// Get text from content of first div - this wouldn't be what real code used
var text = div.textContent;
// Guess the split point as a percentage of the hidden characters
var splitPoint = text.length * div.clientHeight/div.scrollHeight;
// Find first space after splitpoint
splitPoint = text.indexOf(' ', splitPoint);
// Used to make sure we don't get into an infinite loop. Not needed if this code is proven to work on all browsers.
var count = 0;
div.textContent = text.substr(0, splitPoint);
// Add words until we are bigger
while ((div.clientHeight == div.scrollHeight) && (count++ < 1000)) {
splitPoint = text.indexOf(' ', splitPoint + 1);
div.textContent = text.substr(0,splitPoint);
}
// Subtract words until we are smaller
while ((div.clientHeight < div.scrollHeight) && (count++ < 1000)) {
splitPoint = text.lastIndexOf(' ', splitPoint - 1);
div.textContent = text.substr(0, splitPoint);
}
divs[1].textContent = text.substr(splitPoint);
// Create new divs starting at splitpoint (left as an exercise)
div.box {
width: 150px;
height: 150px;
border: 1px solid black;
}
div.moretext {
background: #ccc;
}
<div class="box">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent viverra malesuada nibh, sed luctus eros egestas in. Donec imperdiet, purus a consequat ultricies, mauris magna laoreet neque, quis tincidunt risus augue in lorem. Donec mauris odio, vehicula eget sollicitudin sed, aliquet et metus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec condimentum sed lacus at porttitor. Suspendisse tincidunt vehicula mauris, a ultricies mi fringilla et. Aliquam eget imperdiet magna. Quisque justo libero, commodo vitae dolor quis, volutpat consectetur tellus. Nullam consectetur commodo sem vel posuere. Praesent nec suscipit sem, quis porta diam. Duis gravida lectus fringilla, efficitur quam ut, luctus sem. Ut elit tortor, bibendum a molestie vel, tincidunt vitae lacus. In dapibus commodo mi, id blandit velit mollis placerat. Praesent quis posuere ligula. Praesent ac rhoncus est. In blandit non justo ut egestas. Praesent nec mi sagittis, eleifend nisl ac, egestas enim. Aliquam in tortor ac tellus rutrum feugiat. Etiam bibendum eget metus eget facilisis. Sed mollis eget tellus at viverra. Fusce at leo vel magna convallis dictum. Sed convallis ipsum non feugiat sagittis. Donec a lacus nisl. Donec elementum metus nec arcu interdum interdum. Praesent malesuada eu purus nec tempus. Aliquam in velit nulla. Ut vel turpis et sapien consectetur facilisis. Fusce a pulvinar lorem. Vestibulum vel neque vitae arcu placerat sodales quis ut risus. Cras aliquet, nibh quis sodales venenatis, felis est tempus enim, at condimentum felis nulla sollicitudin tortor. Morbi viverra dolor tempus, lacinia ante ac, dapibus diam. Suspendisse nec ligula viverra, pellentesque lacus nec, maximus turpis.</div>
<div class="moretext"></div>
は多分これは助けることができる:http://stackoverflow.com/questions/143815/determine-if-an-html-elements-content-overflows – huachengzan
@huachengzan感謝を!私はこれが助けることができると思うが、今私はこのようなオーバーフローをテストするために隠し要素を使用しようとするhttp://stackoverflow.com/questions/7738117/html-text-overflow-ellipsis-detection –
Google docsはcontenteditableを使用しません。代わりに、すべてのキー押下をキャプチャし、DOMを手動で更新します。すべての配置は、独自のコードによって実行され、単語をHTMLとして表示するのではなく、絶対的に配置します。 – rjmunro