Using JavaScript to implement a simple message board

Source: Internet
Author: User
Tags delete key

Simple message boards made with native JS.

With the following functions: 1, enter the text message in the input box, 2, the message displayed in the message board, 3, delete the message.

HTML and CSS code:

1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4     <MetaCharSet= "UTF-8">5     <style>6         *{margin:0;padding:0;}7 #container{8 width:500px;9 Height:500px;Ten  One         } A #sendMessage{ - width:500px; - Height:50px; the  - Line-height:50px; - Margin-bottom:30px; -         } + #content{ - width:500px; + Height:400px; A Border:1px solid #808080; at Overflow:Scroll; -         } - #messColumn{width:300px;} -  -     </style> - </Head> in <Body> - <DivID= "Container"> to     <DivID= "SendMessage"> +         <inputID= "Messcolumn"type= "text"> -         <inputID= "BTN"type= "button"value= "message"> the     </Div> *     <!--should put a UL in the content - $     <DivID= "Content"></Div>Panax Notoginseng </Div>
View Code

JS Code:

First, when clicking the Message button, create and add a DIV element in the message board div to pass the text box contents to this element.

Second, add the "delete" button to the div element and add the click event to the Delete.

Third, when you click the Delete button, use the RemoveChild function to delete

1 varMesscol=document.getelementbyid ("Messcolumn");2     varBtn=document.getelementbyid ("BTN");3     varCon=document.getelementbyid ("Content");4btn.onclick=function(){5         if(MessCol.value.trim () = = ""){6Alert ("The input cannot be empty or a space");7             return;8         }9         varMessdiv=document.createelement ("div");TenMessdiv.setattribute ("Style", "width:450px;height:30px;border:2px dotted #808080; margin-bottom:5px;"); One         { A             //This block of code is used to store the code that constructs Messdiv content: message content and style of delete key -             varMesscon = document.createelement ("div"); -Messcon.setattribute ("Style", "Float:left;width:350px;height=30px;line-height:30px;overflow:auto;"); the             vardel = document.createelement ("a"); -Del.setattribute ("Style", "float:left;width:50px;height:30px;line-height:30px;"); -Del.setattribute ("href", "javascript:;"); -del.innerhtml = "Delete"; + Messdiv.appendchild (Messcon); - Messdiv.appendchild (del); +Messcon.innerhtml=Messcol.value; Adel.onclick=function(){ atCon.removechild ( This. parentnode);//let the parent element content Delete the messdiv of the child element -             } -         } - Con.appendchild (messdiv); -Messcol.value= "";//clears the character contents of the input box -}

Using JavaScript to implement a simple message board

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.