即點即改

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>即點即改</title>
	<script src="jq.js"></script>
</head>
<body>
<?php 
$con = array(
	array("id"=>1,"姓名"=>"張三","性別"=>"女"),
	array("id"=>2,"姓名"=>"李四","性別"=>"男"),
	array("id"=>3,"姓名"=>"王五","性別"=>"男"));
 // print_r($con);die;
?>
	<table align="center" border="1">
	<?php foreach ($con as $key => $v): ?>
		<tr pid="<?= $v['id'];?>">
			<td><span class="up" style="cursor:pointer;"><?= $v['姓名'];?></span></td>
			<td><?= $v['性別'];?></td>
		</tr>
	<?php endforeach ?>
	</table>
</body>
</html>
<script>
	//即點即改
	$(document).on("click",".up",function(){
		var content = $(this).text();  //獲取到當前點擊對象的值
		var pid = $(this).parents("tr").attr('pid');   //通過attr 獲取到設置的屬性(pid)
		//當點擊修改文字時 變成文本框並且獲取到原值(content)
		$(this).parent().html("<input type='text' class='fo' value='" + content + "'/>");
		$(".fo").focus();  //光標
		$(".fo").blur(function(){
			//獲取到修改後的值
			var val =  $(".fo").val();
			//
			/*
			將所有修改信息傳到後端
			 */
			$(this).parent().html("<span class='up' style='cursor:pointer;'>"+val+"</span>");
		})
	})
</script>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章