Home Web Front-end HTML Tutorial Set the contenteditable attribute to edit the content of HTML tags (can replace textarea)_HTML/Xhtml_Web page production

Set the contenteditable attribute to edit the content of HTML tags (can replace textarea)_HTML/Xhtml_Web page production

May 16, 2016 pm 04:37 PM


Copy code
The code is as follows:

<div contenteditable="true"> ;You can edit the content inside</div>

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:

 <style type="text/css ">
 .demoEdit{border:1px solid #dddddd;width:450px;min- height:20px;_height:20px;outline:0px;padding:2px;} // outline:0px; style resolution container gets focus When , the container will display the effect of a dotted box in the FF browser.
.demoEdit p{margin:0px;padding:0px;}
style="border:1px solid #dddddd;width:360px;min-height:20px;_height:20px;outline:0px;"></div>
 <div contenteditable="true" class=" demoEdit"></div>

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 <br /> 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>
function img (){
var location1 = prompt("Please enter the address of the image:","http://");
if(location1){
selImg(location1);
}
}
function selImg(s){
if(!s){return false;}
var h=s.substr(s.lastIndexOf(".") 1,3);
if(h=="gif"||h=="jpg" || h=="GIF" || h=="JPG"){
Edit=document.getElementById("idEdit")
Edit.innerHTML ='<img src=' s '>'
}
else{
}
}
</script>
<div NAME=EditCtrl id=idEdit contentEditable=true style="width:100%;height:200px;border:1px solid #666666"> <b>Yuzi.me</b></div>
<input type ="button" name="Submit" value="Insert image" onclick="img()">

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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

How do I use HTML5 form validation attributes to validate user input?

How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

How to efficiently add stroke effects to PNG images on web pages?

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

What is the purpose of the <iframe> tag? What are the security considerations when using it?

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

What is the purpose of the <datalist> element?

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

What is the purpose of the <meter> element?

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

What are the best practices for cross-browser compatibility in HTML5?

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

What is the purpose of the <progress> element?

See all articles