<!DOCTYPE HTML>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
<style type="text/css">
td{
border: #FF0000 1px solid
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
<script>
$.fn.setCursorPosition = function(pos) {
this.each(function(index, elem) {elem.focus();
if (elem.setSelectionRange) {
elem.setSelectionRange(pos, pos);
} else if (elem.createTextRange) {
var range = elem.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
});
return this;
};
$(document)
.ready(function () {
var tds = JSON.parse(localStorage.getItem("tds")) || {};
var td = document.querySelectorAll("td");
Object.keys(tds).forEach(function f(i) {
td[i] && (td[i].innerHTML = tds[i])
});
$("td")
.click(function () {
var td = $(this),
text = td.html(),
$indx = $("td").index(td),
varX = $("<input/>", {
"value": text,
"click": function (event) {
event.stopPropagation()
},
"blur": function () {
tds[$indx] = $(this).val();
localStorage.setItem("tds", JSON.stringify(tds));
td.html($(this).val())
}
});
td.html(varX);
varX.setCursorPosition(text.length)
});
});
</script>
</head>
<body>
<table>
<tr>
<td>1234</td>
<td>1234</td>
</tr>
<tr>
<td>1234</td>
<td>1234</td>
</tr>
<tr>
<td>1234</td>
<td>1234</td>
</tr>
</table>
</body>
</html>