JS dynamically added and deleted UL node LI and related content example _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces how to dynamically add and delete UL node LI in JS and related content. For more information, see The Code is as follows:




  • 111111111111111del

  • 22222222222222222del

  • 333333333333333del

  • 44444444444444444del

  • 555555555555555del



Please select 11111 22222 33333 44444

Script

Function $ (id ){
Var obj = document. getElementById (id );
Return obj;
}

Function del (n ){
$ ('Ul '). removeChild ($ (n ));
}

Function add (id, txt ){
Var ul = $ ('ul ');
Var li = document. createElement ("li ");
Var href_a = document. createElement ("");
Href_a.href = "javascript: del ('" + id + "');";
Href_a.innerHTML = "del ";
Li. innerHTML = txt;
Li. id = id;
Li. appendChild (href_a );
Ul. appendChild (li );
}

Function add_car (obj ){
// Chk ul childNodes length
If ($ ('ul '). childNodes. length <3 ){
Var flag = true;
Var ul_obj =$ $ ('ul '). childNodes;
Var car_id = obj. options [obj. selectedIndex]. value;
Var txt = obj. options [obj. selectedIndex]. text;
If (car_id! = Null & car_id! = ""){

For (var I = 0; I
If (ul_obj [I]. id = car_id ){
Alert ("added! ");
Flag = false;
}
}

If (flag ){
Add (car_id, txt );
}
}

} Else {
Alert ("only three values can be added! ");
Return;
}
}

Function getulvalue (){
If ($ ('ul '). childNodes. length = 0 ){
Alert ("select related content! ");
Return;
} Else {
Var txt = "";
For (var I = 0; I <$ ('ul '). childNodes. length; I ++ ){
Txt + =$ ('ul '). childNodes [I]. id + ",";
}
$ ("Ul_value"). value = txt;
}

}
Script

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.