Use contentEditable in HTML5 to automatically increase the number of lines of text,
ContentEditable is a global attribute developed by Microsoft and decompiled by other browsers and put into application. The main function of this attribute is to allow users to edit the content in the element. Therefore, this element must be an element that can get the focus of the mouse, and an insert symbol should be provided to the user after clicking the mouse, prompt the user to allow editing of the content in this element. The contentEditable attribute is a Boolean attribute and can be specified as true or false.
In addition, this property has a hidden inherit (inheritance) state. when the property is true, the element is specified as allowed to be edited; when the property is false, the element is specified as not allowed to be edited; if true or false is not specified, it is determined by the inherit State. If the parent element of an element is editable, the element is editable.
In addition to the contentEditable attribute, an element also has an isContentEditable attribute. When an element can be edited, the attribute is true. When an element cannot be edited, the attribute is false.
The following is an example of using the contentEditable attribute. When the list element is added with the contentEditable attribute, the element becomes editable. You can test the example in your browser.
Copy XML/HTML Code to clipboard
- <! DOCTYPE html>
- <Head>
- <Meta charset = "UTF-8">
- <Title> conentEditalbe attribute example </title>
- </Head>
- <H2> editable list
- <Ul contentEditable = "true">
- <Li> list element 1 </li>
- <Li> list element 2 </li>
- <Li> list element 3 </li>
- </Ul>
Result of the Code Execution
Automatic increase of Line Text
When talking about multi-line text boxes, we will immediately think of using textarea. It is really convenient to use textarea, but it is a little bad and cannot be automatically increased, you can only specify the number of words in the corresponding columns and rows, or direct css to the height and width.
Automatic increase is required in some cases. For example, the input box similar to Weibo is a typical requirement. The text box has a default height, when the input text exceeds this height, it will automatically increase, and there will be a maximum limit. After this limit is exceeded, a vertical scroll bar will appear.
If textarea is used to meet this requirement, you also need to use js to listen for changes in the text height to dynamically change the height of the text box, which is inconvenient, the contenteditable attribute can be used, especially on mobile terminals.
For example:
Copy XML/HTML Code to clipboard
- <Div contenteditable = "true" class = "box" id = "box">
- </Div>
- <Style>
- . Box {width: 200px; max-height: 100px; border: 1px solid # ccc; overflow-y: auto; overflow-x: hidden ;}
- </Style>
If the value of contenteditable is set to true, the div becomes editable and increases automatically as the content increases. Then, we can set a maximum height for the div to meet the above requirements.