Set the contenteditable attribute to edit the content of HTML tags (can replace textarea)

Set the contenteditable attribute to edit the content of HTML tags (can replace textarea)


Copy code
The code is as follows:

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

If you add contenteditable="true" to BODY, you can find out how magical this attribute is. Therefore, we can set the contenteditable="true" attribute to the HTML tag to edit the tag.

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

Sometimes we can use DIV instead of input or textarea to achieve the same effect. For example, when using ajax, we can get the content of 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 does Div+CSS simulate the textarea text field height adaptation to achieve the contenteditable attribute of the HTML5 standard

This effect is mainly achieved by adding the contenteditable attribute in HTML5 to the tag (contenteditable: specifies whether the user is allowed to edit the content). The great thing is that IE will also support 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 solution: When the container gets the focus, the container will display the effect of a dotted frame in the FF browser.
.demoEdit p{margin:0px;padding:0px;}
</style>
<div contenteditable="true" style="border:1px solid #dddddd;width:360px;min-height:20px;_height:20px;outline:0px;"></div>
<div contenteditable="true" class="demoEdit"></div>

Attachment:
In FF browser, when the container gets the focus, the cursor height will be the same as the container height or the cursor will not be displayed. At this time, if you add a placeholder to the container by default, such as <br/> or &nbsp;, this problem can be solved.

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

The contentEditable attribute in Html can turn on the editable state of certain elements. Maybe you have never used the contentEditable attribute, or even never heard of it, but the role of contentEditable is quite magical. You can make div or the entire web page, as well as span and other elements writable. The most commonly used text input elements are 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? Ha ha…

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

Let's have a special effect. By opening the div element editing, can we insert a picture? This requires the use of js.


Copy code
The code is as follows:

<script>
function img(){
var location1 = prompt("Please enter the address of the picture:","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()">

Awesome! If you want to use more effects, you need to write js code yourself. I hope all the siege heroes can give their best and look forward to sharing!

<<:  jQuery+swiper component realizes the timeline sliding year tab switching effect

>>:  Study on using characters instead of pictures to achieve rounded or sharp corners

Recommend

When MySQL is upgraded to 5.7, WordPress reports error 1067 when importing data

I recently upgraded MySQL to 5.7, and WordPress r...

Personal opinion: Talk about design

<br />Choose the most practical one to talk ...

jQuery+Ajax to achieve simple paging effect

This article shares the specific code of jquery+A...

JavaScript design pattern learning proxy pattern

Table of contents Overview Implementation Protect...

How to use mixins in Vue

Table of contents Preface How to use Summarize Pr...

Sample code for CSS image animation effects (photo frame)

This article introduces the sample code of CSS pi...

How to build a virtual machine with vagrant+virtualBox

1. Introduction Vagrant is a tool for building an...

Vue+Element realizes paging effect

This article example shares the specific code of ...

Windows 10 1903 error 0xc0000135 solution [recommended]

Windows 10 1903 is the latest version of the Wind...

How to configure MySQL on Ubuntu 16.04 server and enable remote connection

background I am learning nodejs recently, and I r...

Using better-scroll component in Vue to realize horizontal scrolling function

About Recently, in the process of learning Vue, I...

Summary of naming conventions for HTML and CSS

CSS naming rules header: header Content: content/c...