Node Deletion compatibility issues in JavaScript.

Source: Internet
Author: User

It is no problem to delete the last node in IE for a JS that deletes a node. However, if you click Delete in Firefox, it will not be deleted until you click Delete again.

 

<HTML>
<Head>
<Title> Delete the element JavaScript removechild </title>
<SCRIPT type = "text/JavaScript">
Function delnode (){
VaR para = Document. getelementbyid ("delul ");
VaR paralen = para. getelementsbytagname ("Li"). length;
If (paralen> 0)
{
Para. removechild (para. lastchild );

} Else {
Alert ("No! ");
}
}
</SCRIPT>
</Head>
<Body>
<Ul id = "delul">
<Li> list1 </LI>
<Li> list2 </LI>
<Li> list3 </LI>
<Li> list4 </LI>
<Li> list5 </LI>
</Ul>
<Input type = "button" value = "delete" onclick = "delnode ();"/>
</Body>
</Html>

 

Changed:

 

Function delnode (){
VaR para = Document. getelementbyid ("delul ");
VaR paralen = para. getelementsbytagname ("Li"). length;
If (paralen> 0)
{
// While is used to support Firefox. Because Firefox considers lastchild as a text node, direct removechild (para. lastchild) does not work.
While (para. lastchild & para. lastchild. nodetype = 3)
{
Para. removechild (para. lastchild );
}

Para. removechild (para. lastchild );

// Another easier method: lastelementchild only supports Firefox
// Para. removechild (para. lastelementchild? Para. lastelementchild: para. lastchild );

} Else {
Alert ("No! ");

Para. removechild (para. lastchild );

}

 

In this way, ie and Firefox have the same effect!

Related Article

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.