這篇文章主要介紹了PHP+jQuery實現即點即改功能,涉及php數組讀取、遍歷及jQuery事件響應與動態修改頁面元素屬性相關操作技巧,需要的朋友可以參考下
本文實例講述了PHP+jQuery實現即點即改功能。分享給大家供大家參考,具體如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>即點即改</title> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.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>
運行結果:
更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP數組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結》、《PHP數據結構與算法教程》、《php程序設計算法總結》、《PHP數學運算技巧總結》、《php字符串(string)用法總結》及《php常見數據庫操作技巧彙總》
希望本文所述對大家PHP程序設計有所幫助。