JavaScript gets the value of the current row in the table (table) deletes the row and increments the row

  • 2020-07-21 06:45:05
  • OfStack


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Js To obtain  table Current row value </title>
<script language=javascript>
	var selectedTr = null;
	function c1(obj) {
		obj.style.backgroundColor = 'blue'; // Put the dot there 1 The row changes the desired color ; 
		if (selectedTr != null)
			selectedTr.style.removeAttribute("backgroundColor");
		if (selectedTr == obj)
			selectedTr = null;// Add this to control click whiten and then click Ash  
		else
			selectedTr = obj;
	}
	/* Gets the top of the selected row 1 The value of the column */
	function check() {
		if (selectedTr != null) {
			var str = selectedTr.cells[0].childNodes[0].value;
			document.getElementById("lab").innerHTML = str;
		} else {
			alert(" Please select a 1 line ");
		}
	}
	/* Delete the selected row */
	function del() {
		if (selectedTr != null) {
			if (confirm(" Are you sure you want to delete it ?")) {
				alert(selectedTr.cells[0].childNodes[0].value);
				var tbody = selectedTr.parentNode;
				tbody.removeChild(selectedTr);
			}
		} else {
			alert(" Please select a 1 line ");
		}
	}
</script>
</head>
<body>
	 Click on the selected Tr , highlight it, and then click Deselect. 
	<input type=button value=" Which one is selected 1 Line? " onclick="check()">
	<input type=button value=" Delete the selected row " onclick="del()">
	<input type=button value=" increase 1 line " onclick="add()">
	<table width="100%" border="1" cellspacing="0" cellpadding="0" id="tab">
		<tr onclick="c1(this);" bgcolor="#cccccc">
			<td><input type="text" value="11"></td>
			<td><input type="text" value="12"></td>
		</tr>
		<tr onclick="c1(this);" bgcolor="#e0e0e0">
			<td><input type="text" value="21"></td>
			<td><input type="text" value="22"></td>
		</tr>
		<tr onclick="c1(this);" bgcolor="#cccccc">
			<td><input type="text" value="31"></td>
			<td><input type="text" value="32"></td>
		</tr>
		<tr onclick="c1(this);" bgcolor="#e0e0e0">
			<td><input type="text" value="41"></td>
			<td><input type="text" value="42"></td>
		</tr>
		<tr onclick="c1(this);" bgcolor="#cccccc">
			<td><input type="text" value="51"></td>
			<td><input type="text" value="52"></td>
		</tr>
	</table>
	<label id="lab"></label>
</body>
</html>


Related articles: