Prototype plugin for dynamically expanding textarea

雨燕双飞 提交于 2019-12-13 12:22:36

问题


Does anyone know of a good plugin for prototype which allows textareas to automatically expand / contract based on how much text there is in them (e.g. a line is added the area gets bigger, a line is removed it gets smaller)?

I need one thats free to use (e.g. some form of GPL type license).


回答1:


This uses Prototype:

<textarea id='t1' cols="40" rows="7" style='overflow:hidden;font-size:14px;font-family:"Times New Roman", Times, serif'></textarea>
<script type="text/javascript">
function activateResize(element) {
    Event.observe(element, 'keyup', function() {
      updateSize(element)
    });
    updateSize(element)
}

function updateSize(element) {
   //if scrollbars appear, make it bigger, unless it's bigger then the user's browser area.
    if(Element.getHeight(element)<$(element).scrollHeight&&Element.getHeight(element)<document.viewport.getHeight()) {
        $(element).style.height = $(element).getHeight()+15+'px'
        if(Element.getHeight(element)<$(element).scrollHeight) {
            window.setTimeout("updateSize('"+element+"')",5)
        }       
    }
}

activateResize('t1')
</script>



回答2:


It's not a plugin, but it's not long: http://www.codelibary.com/JavaScript/Auto%20textarea%20resize.html.

EDIT: found this SO thread



来源:https://stackoverflow.com/questions/511128/prototype-plugin-for-dynamically-expanding-textarea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!