search
HomeWeb Front-endHTML TutorialSet the contenteditable attribute to edit the content of HTML tags (can replace textarea)_HTML/Xhtml_Web page production


Copy code
The code is as follows:



If you add contenteditable="true" to BODY, you can see how magical this attribute is. Therefore, we can set the contenteditable="true" attribute to the HTML tag and the tag can be edited.

The contenteditable attribute is compatible with all browsers (the compatibility of versions prior to IE6 has not been tested)

In some cases, we can use DIV to replace input or textarea to achieve the same effect. For example, when using ajax, we can get the content of the DIV when submitting the form.

Careful people will find that the text box for posting comments in QQ space is actually a DIV, not a textarea text box.

How Div CSS simulates textarea text area height adaptability to meet the HTML5 standard contenteditable attribute

This effect is mainly achieved by adding the contenteditable attribute in HTML5 to the tag (contenteditable: specifies whether users are allowed to edit content). What’s great is that IE also supports this attribute, so you don’t have to worry too much about compatibility issues.
DEMO:


Copy code
The code is as follows:

 

 


Attachment:

Under the FF browser, when the container gets focus, the height of the cursor will be the same as the height of the container or the cursor will not be displayed. At this time, if a placeholder is added to the container by default, such as
or  can solve this problem.

Now Yuzi will introduce to you another good method that is editable and can automatically adapt to the height without adding js code. Let everyone open their eyes. Yuzi can use DIV directly as a text box, similar to the TextArea text box. More importantly, the user experience of DIV is more perfect and handsome.

The contentEditable attribute in Html can turn on the editable state of certain elements. Maybe you have never used the contentEditable attribute, or even heard of it. The role of contentEditable is quite magical. You can make divs or entire web pages, as well as span and other elements writable. Our most commonly used input text content is input and textarea. After using the contentEditable attribute, you can enter content in div, table, p, span, body, and many other elements. In particular, contentEditable has been effectively supported in the html5 standard. Come and witness it.

After setting the contentEditable="true" attribute, isn't it quite magical? Haha...


DEMO page:
http://demo.jb51.net/js/2014/ContentEditable/

Let’s do a special effect. By turning on div element editing, whether we can insert pictures, this requires the use of js.


Copy codeThe code is as follows:
<script><br />function img (){<br />var location1 = prompt("Please enter the address of the image:","http://");<br />if(location1){<br />selImg(location1);<br />}<br />}<br />function selImg(s){<br />if(!s){return false;}<br />var h=s.substr(s.lastIndexOf(".") 1,3);<br /> if(h=="gif"||h=="jpg" || h=="GIF" || h=="JPG"){<br />Edit=document.getElementById("idEdit")<br /> Edit.innerHTML ='<img src=' s ' alt="Set the contenteditable attribute to edit the content of HTML tags (can replace textarea)_HTML/Xhtml_Web page production" >'<br />}<br />else{<br />}<br />}<br /></script>
Yuzi.me



Great, if you want to use more effects, you need to write js code yourself. I hope all the siege heroes will perform to the best of their ability and look forward to sharing!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the root tag in an HTML document?What is the root tag in an HTML document?Apr 29, 2025 am 12:10 AM

TheroottaginanHTMLdocumentis.Itservesasthetop-levelelementthatencapsulatesallothercontent,ensuringproperdocumentstructureandbrowserparsing.

Are the HTML tags and elements the same thing?Are the HTML tags and elements the same thing?Apr 28, 2025 pm 05:44 PM

The article explains that HTML tags are syntax markers used to define elements, while elements are complete units including tags and content. They work together to structure webpages.Character count: 159

What is the significance of <head> and <body> tag in HTML?What is the significance of <head> and <body> tag in HTML?Apr 28, 2025 pm 05:43 PM

The article discusses the roles of <head> and <body> tags in HTML, their impact on user experience, and SEO implications. Proper structuring enhances website functionality and search engine optimization.

What is the difference between <strong>, <b> tags and <em>, <i> tags?What is the difference between <strong>, <b> tags and <em>, <i> tags?Apr 28, 2025 pm 05:42 PM

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

Please explain how to indicate the character set being used by a document in HTML?Please explain how to indicate the character set being used by a document in HTML?Apr 28, 2025 pm 05:41 PM

Article discusses specifying character encoding in HTML, focusing on UTF-8. Main issue: ensuring correct display of text, preventing garbled characters, and enhancing SEO and accessibility.

What are the various formatting tags in HTML?What are the various formatting tags in HTML?Apr 28, 2025 pm 05:39 PM

The article discusses various HTML formatting tags used for structuring and styling web content, emphasizing their effects on text appearance and the importance of semantic tags for accessibility and SEO.

What is the difference between the 'id' attribute and the 'class' attribute of HTML elements?What is the difference between the 'id' attribute and the 'class' attribute of HTML elements?Apr 28, 2025 pm 05:39 PM

The article discusses the differences between HTML's 'id' and 'class' attributes, focusing on their uniqueness, purpose, CSS syntax, and specificity. It explains how their use impacts webpage styling and functionality, and provides best practices for

What is the 'class' attribute in HTML?What is the 'class' attribute in HTML?Apr 28, 2025 pm 05:37 PM

The article explains the HTML 'class' attribute's role in grouping elements for styling and JavaScript manipulation, contrasting it with the unique 'id' attribute.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),