Javascript检测文本区域中的滚动条
我想知道是否有人知道当滚动条出现在Javascript检测文本区域中的滚动条,javascript,mootools,textarea,scrollbar,Javascript,Mootools,Textarea,Scrollbar,我想知道是否有人知道当滚动条出现在textarea中时,我将如何进行检测 我目前正在为我的JavaScript使用mootools,在让它检测滚动条时遇到问题。函数具有滚动条(elem\u id) function has_scrollbar(elem_id) { const elem = document.getElementById(elem_id); if (elem.clientHeight < elem.scrollHeight) alert("T
textarea
中时,我将如何进行检测
我目前正在为我的JavaScript使用mootools,在让它检测滚动条时遇到问题。函数具有滚动条(elem\u id)
function has_scrollbar(elem_id)
{
const elem = document.getElementById(elem_id);
if (elem.clientHeight < elem.scrollHeight)
alert("The element has a vertical scrollbar!");
else
alert("The element doesn't have a vertical scrollbar.");
}
{
const elem=document.getElementById(元素id);
if(元素clientHeight<元素scrollHeight)
警报(“元素有一个垂直滚动条!”);
其他的
警报(“元素没有垂直滚动条。”);
}
看看这个JSFIDLETommaso的解决方案,即使有文本区域,也能完美地工作。但是,如果用户在textarea中键入内容,而textarea突然给了自己一个滚动条,您的javascript就不会知道或者被触发
onKeyUp='has_scrollbar("textareaID")'
我制作了一个jQuery“兼容”版本的代码
因为我找到了
从'react Textarea autosize'导入Textarea;
...
出于好奇,您为什么要检测它?因为我想用它来检测文本框中何时放置了3行文本,并停止允许输入更多字符。非常感谢!太棒了!因为这是一个非常古老的问题,我编辑了这个答案,添加了一个从@Castrohenge代码中分叉的小提琴。如果文本区域有“overflow-y:scroll”,这甚至可以工作!很不错的。
function resize_until_scrollbar_is_gone(selector) {
$.each($(selector), function(i, elem) {
while (elem.clientHeight < elem.scrollHeight) {
$(elem).height($(elem).height()+5);
}
});
}
resize_until_scrollbar_is_gone('textarea');
import Textarea from 'react-textarea-autosize';
...
<Textarea maxRows={3} />