[英]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.