简体   繁体   English

隐藏焦点时隐藏textarea字段

[英]hide textarea field when not in focus

I want to create a function that can hide a <textarea> field when I click at another place. 我想创建一个函数,当我在另一个地方单击时可以隐藏<textarea>字段。 This means that the <textarea> field will hide, and the word(s) inside the <textarea> will remain. 这意味着<textarea>字段将隐藏,和内部的字(一个或多个) <textarea>将保留。

You can view an example on the Mapmarker National Geographic , by clicking on the text button on the left side, writing a message and then click away from the textarea (see below). 您可以在Mapmarker国家地理上查看示例,方法是单击左侧的文本按钮,编写一条消息,然后单击远离文本区域的位置(请参见下文)。
文字按钮

Here is my current code: 这是我当前的代码:

 textarea { background-image: none; } .textlabel-textarea { width: 200px; font-size: inherit!important; color: inherit!important; } .textlabel-text { width: auto; min-width: 200px; max-width: 500px; white-space: nowrap; } .hidden { display: none; } 
 <textarea class="textlabel-textarea rows=" 2 "" [hidden]="!tab1"></textarea <div class="textlabel-text" [hidden]="!tab2"></div> 

This cannot be done with plain CSS, instead it requires Javascript. 普通CSS无法完成此操作,而是需要Javascript。
I utilized jQuery below by simply creating a new <div> with the value (contents) of the <textarea> when you click outside the <textarea> . 当您在<textarea>之外单击时,我通过简单地使用<textarea>的值(内容)创建一个新的<div>来使用jQuery。

 $(document).on('click', function (e) { $('textarea').hide(); $('textarea').after("<div class='text'>" + $('textarea').val() + "</div>"); $(this).unbind("click"); }); $('textarea').on('click', function(e) { e.stopPropagation(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <textarea></textarea> 


Updated version: 更新后的版本:

Since OP commented and said it does need to be editable, and only using HTML and CSS, here is a (much less prettier) version that answers the question: 由于OP评论并说它确实需要可编辑的,并且只能使用HTML和CSS,所以这里有一个(更漂亮的)版本,可以回答这个问题:

 textarea:focus { border: 1px solid #A9A9A9; background: white; resize: both; } textarea { background: 0; border: 0; resize: none; overflow: auto; } 
 <textarea autofocus></textarea> 

 $(".draggable").draggable(); $(document).on('blur', 'textarea', function () { $(this).parent().draggable( 'disable' ); $(this).after("<div class='text'>" + $(this).val() + "</div>").remove(); }); $(document).on('click', '.text', function() { $(this).parent().draggable( 'enable' ); $(this).after("<textarea>" + $(this).html() + "</textarea>").remove(); }); 
 .draggable { border: 1px solid #000; padding: 10px; width: 200px; } 
 <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <div id="1" class="ui-widget-content draggable"> <textarea></textarea> </div> <div id="2" class="ui-widget-content draggable"> <textarea></textarea> </div> <div id="3" class="ui-widget-content draggable"> <textarea></textarea> </div> 

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

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