Use contentEditable in HTML5 to automatically increase the number of lines of text,

Source: Internet
Author: User

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
  1. <! DOCTYPE html>
  2. <Head>
  3. <Meta charset = "UTF-8">
  4. <Title> conentEditalbe attribute example </title>
  5. </Head>
  6. <H2> editable list
  7. <Ul contentEditable = "true">
  8. <Li> list element 1 </li>
  9. <Li> list element 2 </li>
  10. <Li> list element 3 </li>
  11. </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
  1. <Div contenteditable = "true" class = "box" id = "box">
  2. </Div>
  3. <Style>
  4. . Box {width: 200px; max-height: 100px; border: 1px solid # ccc; overflow-y: auto; overflow-x: hidden ;}
  5. </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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.