Home >Web Front-end >JS Tutorial >JS dynamically add and delete UL node LI and related content examples_javascript skills

JS dynamically add and delete UL node LI and related content examples_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:47:271484browse
复制代码 代码如下:



  • 11111111111111111del

  • 22222222222222222del

  • 33333333333333333del

  • 44444444444444444del

  • 55555555555555555del





<script> <br><br>function $$(id){ <br>var obj=document.getElementById(id); <br>return obj; <br>} <br><br>function del(n) { <br>$$('ul').removeChild($$(n)); <br>} <br><br>function add(id,txt) { <br>var ul=$$('ul'); <br>var li= document.createElement("li"); <br>var href_a = document.createElement("a"); <br>href_a.href="javascript:del('" id "');"; <br>href_a.innerHTML ="del"; <br>li.innerHTML=txt; <br>li.id=id; <br>li.appendChild(href_a); <br>ul.appendChild(li); <br>} <br><br>function add_car(obj){ <br>//chk ul childNodes length <br>if($$('ul').childNodes.length<3){ <BR>var flag=true; <BR>var ul_obj=$$('ul').childNodes; <BR>var car_id=obj.options[obj.selectedIndex].value; <BR>var txt=obj.options[obj.selectedIndex].text; <BR>if(car_id!=null&&car_id!=""){ <br><br>for(var i=0;i<ul_obj.length;i ){ <br><br>if(ul_obj[i].id==car_id){ <BR>alert("已经添加!"); <BR>flag=false; <BR>} <BR>} <br><br>if(flag){ <BR>add(car_id,txt); <BR>} <BR>} <br><br>}else{ <BR>alert("只允许加入三个值!"); <BR>return; <BR>} <BR>} <br><br>function getulvalue(){ <BR>if($$('ul').childNodes.length==0){ <BR>alert("请选择相关内容!"); <BR>return; <BR>}else{ <BR>var txt=""; <BR>for(var i=0;i<$$('ul').childNodes.length;i ){ <BR>txt =$$('ul').childNodes[i].id ","; <BR>} <BR>$$("ul_value").value=txt; <BR>} <br><br>} <BR></script>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn