【问题标题】:Expanding Text area- on blur event the text area minimize to its original size扩展文本区域 - 在发生模糊事件时,文本区域最小化为其原始大小
【发布时间】:2015-04-02 13:40:18
【问题描述】:

我必须创建一个根据文本扩展的文本区域。 文本区域最小化到其原始大小的问题(我不希望发生这种情况)。

这是扩展代码:(取自一个previous question)

$('texterea').keyup(function(e){
    if($(this).outerHeight() < this.scrollHeight + parseFloat($(this).css("borderTopWidth")) + parseFloat($(this).css("borderBottomWidth"))) { while($(this).outerHeight() < this.scrollHeight + parseFloat($(this).css("borderTopWidth")) + parseFloat($(this).css("borderBottomWidth"))) { $(this).height($(this).height()+1); }; } else { while(!($(this).outerHeight() < this.scrollHeight + parseFloat($(this).css("borderTopWidth")) + parseFloat($(this).css("borderBottomWidth")))) { $(this).height($(this).height()-1); }; 

html:

<g:form action="reply" id="${conversationInstance.id}">
    <div class="convo-body">
        <textarea class="reply input-text-big" rows="1" name="message" placeholder="Send a reply"></textarea>
    </div>
    <div class="mt-16">
        <button type="submit" class="pull-right button-blue" disabled>Send</button>
    </div>
</g:form>

【问题讨论】:

  • 哇。也许格式化一点。并且更清楚你在问什么。包括你的 html 或小提琴

标签: jquery textarea expand


【解决方案1】:

好的,那段代码伤了我的脑筋,所以这里是它的编辑:

$('textarea').keydown(function(e){
    var $this = $(this),
        bw = parseFloat($this.css('border-top-width')) + 
            parseFloat($this.css('border-bottom-width')),
        thisTargetH = this.scrollHeight,
        thisOrigH = $this.outerHeight() - bw,
        diff = thisTargetH-thisOrigH;
    if( diff > 0 ){
         $this.height('+='+diff); 
    }
});

有关工作示例,请参阅 this demo fiddle

【讨论】:

  • 文本区域没有展开
  • @saritrotshild 哎呀,修复了 anwser 代码和链接的演示小提琴。现在可以使用了。
  • @Ted- 如何移除卷轴?
  • @saritrotshild 只是将溢出设置为隐藏,例如textarea{ overflow:hidden; }
  • @Ted- 10x 为您提供帮助....但是由于某种原因,当我在文本区域外单击时它仍然最小化:(
猜你喜欢
  • 2011-07-31
  • 2012-06-27
  • 1970-01-01
  • 2011-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
  • 1970-01-01
相关资源
最近更新 更多