jquery prop(“outerHTML“) 获取当前标签和标签内部的html 代码

本文介绍使用jQuery实现动态添加和删除表格行的方法,通过复制模板行并修改元素ID和名称,确保每行唯一性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原生JS DOM里有一个内置属性 outerHTML,用来获取当前节点的html代码(包含当前节点),所以用jQuery的prop()能拿到。

$(".test").prop("outerHTML");

jquery设置outerhtml
$('.test').prop('outerHTML', '<div>');

https://blog.csdn.net/yh12346789/article/details/79781204

示例1、jquery 动态添加一行:

代码

<!DOCTYPE html>
<html>
<head>
<title> jquery 动态添加一行</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Expires" content="0">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Cache" content="no-cache">

<style type="text/css">
html {
    font-family: sans-serif;
    -ms-text-size-adjust: 100%;
    -webkit-text-size-adjust: 100%;
}
 
body {
    margin: 10px;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}
 
td,th {
    padding: 0;
}
 
.pure-table {
    border-collapse: collapse;
    border-spacing: 0;
    empty-cells: show;
    border: 1px solid #cbcbcb;
}
 
.pure-table caption {
    color: #000;
    font: italic 85%/1 arial,sans-serif;
    padding: 1em 0;
    text-align: center;
}
 
.pure-table td,.pure-table th {
    border-left: 1px solid #cbcbcb;
    border-width: 0 0 0 1px;
    font-size: inherit;
    margin: 0;
    overflow: visible;
    padding: .5em 1em;
}
 
.pure-table thead {
    background-color: #e0e0e0;
    color: #000;
    text-align: left;
    vertical-align: bottom;
}
 
.pure-table td {
    background-color: transparent;
}
 
.pure-table-bordered td {
    border-bottom: 1px solid #cbcbcb;
}
 
.pure-table-bordered tbody>tr:last-child>td {
    border-bottom-width: 0;
}
</style>
<script type="text/javascript" src="jquery-2.1.1.min.js"></script>
	
<script type="text/javascript">

// 复制一行
function cpLine(){
	var line=$("#line").prop("outerHTML");
	//console.log(line)  // 打印 html
		
	// 将内容添加到table的最后一行
	$(line).appendTo("table");		
}

// 删除一行
function delLine(obj){
	$(obj).parents("tr").remove();				
}
</script>

</head>
<body>	
	
	<input type="button" value="复制一行" onclick="cpLine()" >
	
	<table  class="pure-table pure-table-bordered" >
		<thead>
			<tr>				
				<td>性别</td>
				<td>年龄</td>
				<td>备注</td>
				<td>操作</td>
			</tr>
		</thead>
		<tbody>
			<tr  id="line"  >				
				<td class="sex" >				
					<select id="sex" name="sex" >
						<option value="">--请选择--</option>
						<option></option>
						<option></option>
					</select>
				</td>
				<td class="age">
					<input type="text" name="age"  id="age" >
				</td>
				<td  class="remark">
					<input type="text" name="remark"  id="remark" >
				</td>
				<td>
					<input type="button" value="删除" onclick="delLine(this)" >
				</td>
			</tr>
		</tbody>
	</table>	
	
</body>
</html>

示例2、jquery 动态添加一行,并修改input的id,name的值:

代码

<!DOCTYPE html>
<html>
<head>
<title> jquery 动态添加一行,并修改input的id,name的值 </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Expires" content="0">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Cache" content="no-cache">

<style type="text/css">
html {
    font-family: sans-serif;
    -ms-text-size-adjust: 100%;
    -webkit-text-size-adjust: 100%;
}
 
body {
    margin: 10px;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}
 
td,th {
    padding: 0;
}
 
.pure-table {
    border-collapse: collapse;
    border-spacing: 0;
    empty-cells: show;
    border: 1px solid #cbcbcb;
}
 
.pure-table caption {
    color: #000;
    font: italic 85%/1 arial,sans-serif;
    padding: 1em 0;
    text-align: center;
}
 
.pure-table td,.pure-table th {
    border-left: 1px solid #cbcbcb;
    border-width: 0 0 0 1px;
    font-size: inherit;
    margin: 0;
    overflow: visible;
    padding: .5em 1em;
}
 
.pure-table thead {
    background-color: #e0e0e0;
    color: #000;
    text-align: left;
    vertical-align: bottom;
}
 
.pure-table td {
    background-color: transparent;
}
 
.pure-table-bordered td {
    border-bottom: 1px solid #cbcbcb;
}
 
.pure-table-bordered tbody>tr:last-child>td {
    border-bottom-width: 0;
}
</style>
<script type="text/javascript" src="jquery-2.1.1.min.js"></script>
	
<script type="text/javascript">

// 复制一行
function cpLine(){
	var line=$("#line").prop("outerHTML");
	//console.log(line)  // 打印 html
	
	//获取当前最后一行的序号的值
	var num=$("table").find("tr:visible:last").find(".num").text();
	//console.log("num = "+num)
	if(undefined == num){
		num=0;
	}
	
	num=parseInt(num)+1; // 序号加 1
	
	var result = $(line).show()
	.find(".num").html(num).end() //序号	
	.find(".sex").find("#sex").prop("name","sex"+num).prop("id","sex"+num).end().end()  //修改性别中的id、name
	.find(".age").find("#age").prop("name","age"+num).prop("id","age"+num).end().end()  //修改年龄中的id、name
	.find(".remark").find("#remark").prop("name","remark"+num).prop("id","remark"+num).end().end()   //修改备注中的id、name 
	
	// 打印修改后的 html
	console.log($(result).prop("outerHTML"))
	
	// 将修改后的内容添加到table的最后一行
	$(result).appendTo("table");		
}

// 删除一行
function delLine(obj){
	$(obj).parents("tr").remove();				
}
</script>

</head>
<body>	
	
	<input type="button" value="复制一行" onclick="cpLine()" >
	
	<table  class="pure-table pure-table-bordered" >
		<thead>
			<tr>
				<td>序号</td>
				<td>性别</td>
				<td>年龄</td>
				<td>备注</td>
				<td>操作</td>
			</tr>
		</thead>
		<tbody>
			<tr  id="line" style="display:none" >
				<td class="num">
					0
				</td>
				<td class="sex" >				
					<select id="sex" name="sex" >
						<option value="">--请选择--</option>
						<option></option>
						<option></option>
					</select>
				</td>
				<td class="age">
					<input type="text" name="age"  id="age" >
				</td>
				<td  class="remark">
					<input type="text" name="remark"  id="remark" >
				</td>
				<td>
					<input type="button" value="删除" onclick="delLine(this)" >
				</td>
			</tr>
		</tbody>
	</table>
	
	
</body>
</html>

运行结果:

在这里插入图片描述

说明

动态添加一行前,首先要写出一行,并将其隐藏,
然后每次添加时,复制隐藏行的代码,并在代码的基本上进行修改。

每行中的序号、性别、年龄、年龄 input 标签中的id和name,通过序号加1的方式,让其都是唯一的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值