檢查內容是否溢出
回答
這是一個jQuery插件適合文本的寬度和高度:
(function($) {
$.fn.fitText = function(options) {
options = $.extend({
width: 0,
height: 0
}, options);
$(this).each(function() {
var elem = $(this);
if (options.height > 0) {
while (elem.height() > options.height) {
elem.text(elem.text().substring(0, (elem.text().length - 4)) + 'YourLink');
}
}
if (options.width > 0) {
while (elem.width() > options.width) {
elem.text(elem.text().substring(0, (elem.text().length - 4)) + 'YourLink');
}
}
});
}
})(jQuery);
如果創建這樣的結構:
<div id="outer" style="overflow: auto">
<div id="inner">
content
</div>
</div>
然後溢出發生時inner
的寬度或高度超過的outer
因爲外假定的尺寸視口和內部呈現顯示所有內容所需的最小寬度和高度。
您可以將outer
標記爲visibility: hidden
,以使其佈局但不顯示。
如果內容包含position: fixed
內容,那麼該部分將不會被考慮(並且在CSS 2上甚至不會被裁剪)。
問題被標記爲Javascript! – Gerben
@Gerben,請您擴展一下嗎?這個問題明確地談到了DIV中的HTML內容。第一個標籤如何與JavaScript相關? –
使用jQuery和Marquee Text When Text Overflows:
$('div').each(function(){
var $this = $(this);
if ($this.get(0).scrollHeight > $this.height()) {
$this.after('<a href="#" target="new">Read More</a>');
}
});
沒有jQuery的答案:
if(elements.scrollHeight > element.clientHeight)
alert('content-overflow')//not to be confused with stackoverflow
似乎IE6和IE7有時會給scrollHeight提供不正確的值。你警告。 – Gerben
- 1. 檢查WrapPanel是否溢出?
- 2. JS檢查內容溢出verticaly
- 3. 檢測溢出內容
- 4. javascript css檢查是否溢出
- 5. 檢查MenuItem是否在ActionBar溢出
- 6. 如何檢查溢出是否發生?
- 7. 帶FOP的xslfo:檢查內容是否溢出並調用不同的模板?
- 8. 檢查溢出
- 9. 檢測CSS中的內容溢出
- 10. Div內容溢出
- 11. Div內容溢出
- 12. 溢出的內容
- 13. 檢查C++下溢/溢出?
- 14. 檢查DataInputStream是否有內容
- 15. 檢查內容是否在iframe中?
- 16. 檢查內容是否在web.config中
- 17. 檢查元素是否包含內容
- 18. 檢查tinyMCE是否有內容
- 19. 檢查網上是否有新內容
- 20. 檢查ServletResponse是否有任何內容
- 21. 檢查看到的元素有內容溢出
- 22. strtoXX:檢查溢出
- 23. MIPS溢出檢查
- 24. DIV定位,內容總是溢出
- 25. 內容溢出內容區域
- 26. 檢測文本是否溢出
- 27. 檢測元素是否進入溢出
- 28. 檢測是否溢出沒有javascript
- 29. 強制滾動條顯示內容是否溢出
- 30. 如何判斷UIScrollView內容是否溢出
這可能有所幫助:http://stackoverflow.com/questions/835684/marquee-text-when-text-overflows –