以下的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