[英]Detecting when a scrollbar appears on texarea
我知道这里已经问过这个问题。 基本上,该解决方案有效,但它们使用的是旧版本的jquery,并且该解决方案是使用不推荐使用的功能实现的。 因此,根据该问题的答案和问题,我尝试迁移到新版本。
因此,基本上,使用旧版jquery的解决方案是此解决方案。
$('textarea.paginate').live('keydown', function(event) {
if (this.clientHeight < this.scrollHeight) {
alert("Please Something);
}
});
这是我向on函数的迁移,但两种方法均无效。 该示例的演示: http : //jsbin.com/saxay/1/edit
我做错了什么?
$("textarea").on('keyup','.note-codable',function(event){
if(event.keyCode == 13) { }
if (this.clientHeight < this.scrollHeight) {
alert("Please Something");
}
});
$(document).on('keyup','.note-codable',function(event){
if(event.keyCode == 13) { }
if (this.clientHeight < this.scrollHeight) {
alert("Please Something");
}
});
更新
试试这个方法: DEMO
$.fn.hasVerticalScrollBar = function() {
if (this[0].clientHeight < this[0].scrollHeight) {
return true
} else {
return false
}
};
$(document).on('keydown','.note-codable',function(event){
if(event.keyCode == 13) { }
if ($(this).hasVerticalScrollBar()) {
alert("Please Something");
}
});
另外,如果您注意到我将keyup
事件更改为keydown
,这在我看来更好,因为用户将手指按住按钮时,如果在keyup
事件上不会触发代码。
解决了您的问题
您只需在第一种方法中在live
位置使用.on
。
$('textarea.paginate').on('keydown', function(event) {
// scrollbars apreared
if (this.clientHeight < this.scrollHeight) {
alert("Please add a new card for having a better format. Remember this is a WYSIWYG");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.