简体   繁体   English

如何通过javascript doubleclick增加textarea的大小?

[英]How can I increase the size of textarea via javascript doubleclick?

I am trying to create a trigger in my form such that if someone double-clicks the empty part of textarea, it increases the height. 我正在尝试在表单中创建一个触发器,以便如果有人双击文本区域的空白部分,则会增加高度。 I am trying the below code but it doesn't work. 我正在尝试下面的代码,但是它不起作用。 What am I missing? 我想念什么?

HTML 的HTML

<div id="div_details" class="fields">
<label id="label_details" for="input_details" >Details</label><br/>
<textarea id="input_details" class="" name="details" disabled="disabled" >Customer can look more.</textarea>

JavaScript 的JavaScript

$('#input_details').dblclick(function(){
    $('#input_details').animate({height: '+=50'}, 500);
});

jsfiddle jsfiddle

The problem is that disabled form elements don't fire mouse events. 问题在于禁用的表单元素不会触发鼠标事件。 Your code would work if it were applied to a text area that wasn't in a disabled state. 如果将代码应用于未处于禁用状态的文本区域,则代码将起作用。

One possible solution is to surround the text area in a container, which you animate instead, and have the text area set to fill the container. 一种可能的解决方案是将文本区域包围在容器中,而对其进行动画处理,并设置文本区域以填充容器。

An example: 一个例子:

 $('.container').dblclick(function(){ $('.container').animate({height: '+=50'}, 500); }); 
 .container{ height:100px; } #input_details{ height:100%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="div_details" class="fields"> <label id="label_details" for="input_details" >Details</label><br/> <div class="container"> <textarea id="input_details" class="" name="details" disabled="disabled" >Customer can look more.</textarea> </div> 

Put a div around the textare and bind the event to this div. 在textare周围放置一个div,然后将事件绑定到该div。 It´s not possible to do this with a disabled textarea. 禁用文本区域是不可能做到这一点的。

Example: (binded to your div) 示例:(绑定到您的div)

$('#div_details').dblclick(function(){
    $('#input_details').animate({height: '+=50'}, 500);
});

You've disabled your textarea, which means the double click event is never triggered. 您已禁用文本区域,这意味着永远不会触发双击事件。

A form control that is disabled must prevent any click events that are queued on the user interaction task source from being dispatched on the element. 禁用的表单控件必须防止在用户交互任务源上排队的任何单击事件在元素上分派。 ( https://www.w3.org/TR/html5/forms.html#attr-fe-disabled ) https://www.w3.org/TR/html5/forms.html#attr-fe-disabled

Once you remove the disabled attribute, you'll see it starts working. 删除disabled属性后,您将看到它开始起作用。 I'd suggest to not use a textarea if you're not planning to make the element interactive. 如果您不打算使该元素具有交互性,建议不要使用textarea

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM