Home > Web Front-end > JS Tutorial > body text

js implements web page collection function_javascript skills

WBOY
Release: 2016-05-16 15:25:14
Original
1476 people have browsed it

The example in this article describes the method of dynamically adding or deleting URLs using js. Share it with everyone for your reference, the details are as follows:

Operation rendering:

The specific code is as follows

<html>
 <head>
 <title>脚本之家</title>
 <meta charset="utf-8">
 <script>
 function add(){
  var name = document.getElementById("name").value;
  var url = document.getElementById("url").value; 
  var list = document.getElementById("list");
  //动态创建节点
  var link = document.createElement("a");
  link.setAttribute("href",url); //设置属性
  link.innerHTML = name; 
  //增加删除的按钮
  var button = document.createElement("input");
  button.setAttribute("type","button");
  button.value = "删除";
  /**
  (1).event对象表示对象的状态,提供了对象的相关细节,IE浏览器被
  存储在 Window 对象的 event 属性中。
  (2).srcElement属性是对于生成事件的 Window 对象、Document 对象或 Element 对象的引用 
  (3).parentNode 属性返回指定节点的父节点。
  (4).removeChild() 方法删除子节点。
 
  **/
  button.onclick = function(event){
  var target;
  if (event == null)
  {
   target = window.event.srcElement;
  }else{
   target = event.target;
  }
  var div = target.parentNode;
  div.parentNode.removeChild(div);
  alert("删除成功");
  }
  //添加生成的内容
  var div = document.createElement("div");
  div.appendChild(button);
  div.insertBefore(link,button);
  list.appendChild(div);
 
  }
 </script>
 </head>
 <body>
 <hr>
 站点名称:<input type="text" name="name" id="name">
 网址:<input type="text" name="url" id="url">
 <input type="button" value="增加" onclick="add()">
 <div id="list">
 </div>
 </body>
</html>
Copy after login

I hope this article will be helpful to everyone in JavaScript programming.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!