【html+js】td元素可编辑,绑定事件并可读取当前对象



 

td div等元素可编辑,只需要加入属性contentEditable='true';

绑定事件,并获取this,可以通过var that=this;来获取

<body>
<table id="table" cellpadding="2px" border=1 cellspacing=0></table>
</body>
<script>
var tdValue='';
window.onload = function(){
  $.ajax({
          type: "GET",
          url: "/dev/data",
          data: "",
          success: function(msg){
                str = "<tr><th>ID</th><th>IP地址</th><th>用户名</th><th>登录密码</th><th>备注</th><th>设备类型</th><th>是否收集</th>";
                for(var i=0; i< msg['device'].length; i++){
                  str += "<tr>";
                  str += "<td>"+msg['device'][i]["id"]+"</td>";
                  str += "<td contentEditable='true'>"+msg['device'][i]["ip"]+"</td>";
                  str += "<td contentEditable='true'>"+msg['device'][i]["user"]+"</td>";
                  str += "<td contentEditable='true'>"+msg['device'][i]["password"]+"</td>";
                  str += "<td contentEditable='true'>"+msg['device'][i]["info"]+"</td>";
                  str += "<td contentEditable='true'>"+msg['device'][i]["type"]+"</td>";
                  if(typeof(msg['device'][i]["used"])=="undefined"){
                    str += "<td>false</td>";
                  }
                  else{
                    str += "<td>"+msg['device'][i]["used"]+"</td>";
                  }
                  str += "</tr>";
                }
                  $("#table").html(str);
                  $("td").bind("focus",function (){
                    var that = this;
                    tdValue = $(that).html();
                  });
                  $("td").bind("blur",function (){
                    var that = this;
                    if (tdValue !=$(that).html()){
                      ajax_td_new_value(that);
                    }
                  });
           },
          error: function(msg){
             $("#table").html(msg);
             }
         });
}
function ajax_td_new_value(item){
  alert($(item).parent().html());
}
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值