Home>

If I want to create a div element.

1. Create using dom object:

Create an element using the document.createelement ("div") method.

2. Create using jquery:

Use $("

New element created by jquery
") to create the element directly.

If necessary, delete the div element whose id is "div2js".

1. Use the dom object

First you need to find the parent element of the deleted element,Delete the child elements that need to be deleted through the parent element.

var el=document.getelementbyid ("div2js");
 el.parentnode.removechild (el);

2. Use jquery

Find and delete directly.

$("#div2js"). remove ();

Finally we look at an example

<script type="text/javascript" language="javascript">
function inputonblur ()
{var name=document.getelementbyid ("name"). value;
     if (name.length>10 || name.length<2)
       {
        var element=document.getelementbyid ("message");
      if (element)
      {
      //alert(element.parentnode.innerhtml);
      element.parentnode.removechild (element);
      }
               var myspan=document.createelement ("span");
               document.getelementbyid ("containers"). appendchild (myspan);
               myspan.id="message";
               myspan.innerhtml="<img src =" false.jpg "/>Please enter the correct name";
        }
     else {
      var element=document.getelementbyid ("message");
      if (element)
      {
      //alert(element.innerhtml);
      element.parentnode.removechild (element);
      }
               var myspan=document.createelement ("span");
               document.getelementbyid ("containers"). appendchild (myspan);
               myspan.id="message";
               myspan.innerhtml="<img src =" true.gif "/>the username is entered correctly";
          }
}
</script>
<div>
Name:<input type="text" onblur="inputonblur ()" /><span></span></div>
<script language="javascript">
document.getelementbyid ("containers"). innerhtml="<font color=red>please enter name</font>";
  • Previous Detailed Java file and directory management and input-output related operations
  • Next PHP use array_slice function to get random array or first few data