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