以下的jQuery插件会通知结果。
CSS
#tempDiv{
height:10px;
overflow:hidden;
}
为了确定宽度溢出,
(function($) {
$.fn.isOverflowWidth = function() {
return this.each(function() {
var el = $(this);
if (el.css("overflow") == "hidden") {
var text = el.html();
var t = $(this.cloneNode(true)).hide().css('position', 'absolute').css('overflow', 'visible').width('auto').height(el.height());
el.after(t);
function width() {
return t.width() > el.width();
};
alert(width());
}
});
};
})(jQuery);
为了确定高度溢出,
(function($) {
$.fn.isOverflowHeight = function() {
return this.each(function() {
var el = $(this);
if (el.css("overflow") == "hidden") {
var text = el.html();
var t = $(this.cloneNode(true)).hide().css('position', 'absolute').css('overflow', 'visible').height('auto').width(el.width());
el.after(t);
function height() {
return t.height() > el.height();
};
alert(height());
}
});
};
})(jQuery);
http://jsfiddle.net/C3hTV/
你是什么意思的“检测”到底是什么?你想做什么反应,显示一个滚动条? –
如果文本溢出,我想调整div在鼠标悬停上的大小,但我已经对它进行了排序,因此它不是问题的一部分。 –
类似的老问题,很好的回答:https://stackoverflow.com/a/143889/573057 – earcam