视频

原生

正则返回什么
正则对照表

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>添加删除记录练习</title>
	<link rel="stylesheet" type="text/css" href="css.css" />
</head>
<body>

	<table id="employeeTable">
		<tr>
			<th>Name</th>
			<th>Email</th>
			<th>Salary</th>
			<th>&nbsp;</th>
		</tr>
		<tr>
			<td>Tom</td>
			<td>tom@tom.com</td>
			<td>5000</td>
			<td><a href="deleteEmp?id=001">Delete</a></td>
		</tr>
		<tr>
			<td>Jerry</td>
			<td>jerry@sohu.com</td>
			<td>8000</td>
			<td><a href="deleteEmp?id=002">Delete</a></td>
		</tr>
		<tr>
			<td>Bob</td>
			<td>bob@tom.com</td>
			<td>10000</td>
			<td><a href="deleteEmp?id=003">Delete</a></td>
		</tr>
	</table>

	<div id="formDiv">
	
		<h4>添加新员工</h4>

		<table>
			<tr>
				<td class="word">name: </td>
				<td class="inp">
					<input type="text" name="empName" id="empName" />
				</td>
			</tr>
			<tr>
				<td class="word">email: </td>
				<td class="inp">
					<input type="text" name="email" id="email" />
				</td>
			</tr>
			<tr>
				<td class="word">salary: </td>
				<td class="inp">
					<input type="text" name="salary" id="salary" />
				</td>
			</tr>
			<tr>
				<td colspan="2" align="center">
					<button id="addEmpButton" value="abc">
						Submit
					</button>
				</td>
			</tr>
		</table>

	</div>

	<script type="text/javascript">
		/*
		 功能说明:
			 1. 点击'Submit', 根据输入的信息在表单中生成一行员工信息
			 2. 点击Delete链接, 提示删除当前行信息, 点击确定后删除信息
		 技术要点:
			 1. DOM查询
			 2. 绑定事件监听
			 3. DOM增删改
			 5. 取消事件的默认行为
		 */
		function removeTr(){
			var trNode = this.parentNode.parentNode;
			var tds = trNode.getElementsByTagName("td");
			var nameStr = tds[0].firstChild.nodeValue;
			var flag = confirm("真的要删除" + nameStr + "的信息吗?");
			if(flag){
				trNode.parentNode.removeChild(trNode);//这里要找到父元素才能调用删节点函数
			}

			return false;//消除取消的影响
		}

		window.onload = function(){
			//目标1:点击Delete删除记录
			var aEles = document.getElementsByTagName("a");
			for(var i = 0;i < aEles.length;i++){
				aEles[i].onclick = removeTr;
			}

			//目标2:点击Submit增加记录
			var subBtn = document.getElementById("addEmpButton");
			subBtn.onclick = function(){
				//1.收集输入的数据
				var nameText = trim(document.getElementById("empName").value);
				var emailText = trim(document.getElementById("email").value);
				var salaryText = trim(document.getElementById("salary").value);

				document.getElementById("empName").value = nameText;//去除文本中多余空格
				document.getElementById("email").value = emailText;
				document.getElementById("salary").value = salaryText;

				if(nameText == "" || emailText == "" || salaryText == ""){
					alert("您输入的内容不完整");
					return ;
				}

				//组装节点
				var nameTd = document.createElement("td");
				nameTd.appendChild(document.createTextNode(nameText));
				var emailTd = document.createElement("td");
				emailTd.appendChild(document.createTextNode(emailText));
				var salaryTd = document.createElement("td");
				salaryTd.appendChild(document.createTextNode(salaryText));
				var aTd = document.createElement("td");
				var aNewEle = document.createElement("a");
				aNewEle.href = "deleteEmp?id=XXX";
				aNewEle.appendChild(document.createTextNode("Delete"));
				aNewEle.onclick = removeTr;
				aTd.appendChild(aNewEle);

				var trNode = document.createElement("tr");
				trNode.appendChild(nameTd);
				trNode.appendChild(emailTd);
				trNode.appendChild(salaryTd);
				trNode.appendChild(aTd);

				var empTable = document.getElementById("employeeTable");
				empTable.appendChild(trNode);
			}

			function trim(str){
				var reg = /^\s*|\s*$/g;//意为去除目标字符串左右两边的空白字符
				//表达式加上参数g之后,表明可以进行全局匹配
				//replace 一个在字符串中执行查找匹配的String方法,并且使用替换字符串替换掉匹配到的子字符串。
				return str.replace(reg,"");
				/*
				replace() 方法通过用 newChar 字符替换字符串中出现的所有 searchChar 字符,并返回替换后的新字符串。
				public String replace(char searchChar, char newChar) 
				*/
			}
		}
	</script>
</body>
</html>

jQuery

event.preventDefault()Event 接口的 preventDefault()方法,告诉user agent:如果此事件没有被显式处理,它默认的动作也不应该照常执行。此事件还是继续传播,除非碰到事件侦听器调用stopPropagation() 或stopImmediatePropagation(),才停止传播。

id是给服务端的用的。

事件委托优化下列代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>添加删除记录练习</title>
  <link rel="stylesheet" type="text/css" href="css.css"/>
</head>
<body>

<table id="employeeTable">
  <tr>
    <th>Name</th>
    <th>Email</th>
    <th>Salary</th>
    <th>&nbsp;</th>
  </tr>
  <tr>
    <td>Tom</td>
    <td>tom@tom.com</td>
    <td>5000</td>
    <td><a href="deleteEmp?id=001">Delete</a></td>
  </tr>
  <tr>
    <td>Jerry</td>
    <td>jerry@sohu.com</td>
    <td>8000</td>
    <td><a href="deleteEmp?id=002">Delete</a></td>
  </tr>
  <tr>
    <td>Bob</td>
    <td>bob@tom.com</td>
    <td>10000</td>
    <td><a href="deleteEmp?id=003">Delete</a></td>
  </tr>

</table>

<div id="formDiv">

  <h4>添加新员工</h4>

  <table>
    <tr>
      <td class="word">name:</td>
      <td class="inp">
        <input type="text" name="empName" id="empName"/>
      </td>
    </tr>
    <tr>
      <td class="word">email:</td>
      <td class="inp">
        <input type="text" name="email" id="email"/>
      </td>
    </tr>
    <tr>
      <td class="word">salary:</td>
      <td class="inp">
        <input type="text" name="salary" id="salary"/>
      </td>
    </tr>
    <tr>
      <td colspan="2" align="center">
        <button id="addEmpButton" value="abc">
          Submit
        </button>
      </td>
    </tr>
  </table>
</div>

<script src="jquery-1.10.1.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
  /*
   功能说明:
   1. 点击'Submit', 根据输入的信息在表单中生成一行员工信息
   2. 点击Delete链接, 提示删除当前行信息, 点击确定后删除信息
   技术要点:
   1. DOM查询
   2. 绑定事件监听
   3. DOM增删改
   4. 取消事件的默认行为
   */
  $(function () {
    $('#addEmpButton').click(function () {
      //1.收集输入的数据
      var $empName = $('#empName')
      var $email = $('#email')
      var $salary = $('#salary')

      var empName = $empName.val()
      var email = $email.val()
      var salary = $salary.val()
      var id = Date.now()//随机数

      //2.生成对应的<tr>标签结构,并插入#employeeTable的tbody中
      /*
       <tr>
         <td>Bob</td>
         <td>bob@tom.com</td>
         <td>10000</td>
         <td><a href="deleteEmp?id=003">Delete</a></td>
       </tr>
       */
      $('<tr></tr>')
        .append('<td>'+empName+'</td>')//拼串
        .append('<td>'+email+'</td>')
        .append('<td>'+salary+'</td>')
        .append('<td><a href="deleteEmp?id='+id+'">Delete</a></td>')
        .appendTo('#employeeTable')
        .find('a')
        .click(clickA)//给a标签加点击监听,阻止自带的行为
      
      //3.清除
      $empName.val('')
      $email.val('')
      $salary.val('')

    })
    //给所有删除链接绑定点击监听
    $('a').click(clickA)

    function clickA (event) {
      event.preventDefault()
      var $tr = $(this).parent().parent()
      var name = $tr.children('td:first').html()
      if(confirm('确定删除'+name+'吗?')) {
        $tr.remove()
      }
    }
  })


</script>
</body>
</html>

效果展示

posted on 2023-01-13 23:40  垂序葎草  阅读(26)  评论(0编辑  收藏  举报