Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将使用jQuery编辑的HTML表保存到本地存储?_Javascript_Jquery_Html_Offline - Fatal编程技术网

Javascript 如何将使用jQuery编辑的HTML表保存到本地存储?

Javascript 如何将使用jQuery编辑的HTML表保存到本地存储?,javascript,jquery,html,offline,Javascript,Jquery,Html,Offline,我有一个html表,用户可以向其中添加单独的行。我希望用户能够脱机保存此数据,以便在刷新后保留表上的数据。有人告诉我这是最简单的方法,但如果有人有任何其他建议,我都愿意倾听 $(document).ready(function(){ var count = 1; var table = "You fucked up yo"; $('#add').click(function addRow(){ var model = $('#Model').val(); var size = $('#S

我有一个html表,用户可以向其中添加单独的行。我希望用户能够脱机保存此数据,以便在刷新后保留表上的数据。有人告诉我这是最简单的方法,但如果有人有任何其他建议,我都愿意倾听

$(document).ready(function(){
var count = 1;
var table = "You fucked up yo";
$('#add').click(function addRow(){
  var model = $('#Model').val();
  var size = $('#Size').val();
  var resolution = $('#Resolution').val();
  var backlight = $('#Backlight').val();
  var pins = $('#Pins').val();
  var touch = $('#Touch').val();
  $('#screens').append("<tr id='row"+count+"'><td>"+count+"</td><td id 
  ='model"+count+"'>"+model+"</td><td id ='sizes"+count+"'>"+size+"</td><td 
  id 
  ='resolution"+count+"'>"+resolution+"</td><td id 
  ='backlight"+count+"'>"+backlight+"</td><td id ='pins"+count+"'>"+pins+"
  </td><td id ='touch"+count+"'>"+touch+"</td>");
  var key = "table"+count;
  count++;
});
$('#remove').click(function removeRow(){
  var row = $('#row').val();
  $('table#screens tr#row'+row).remove();
});
$('#save').click(function save(){
  var table = $('#screens').html;
  localStorage.setItem("Table", table);
  alert('yo');
});
$('#open').click(function open(){
  $('#screens').html =  localStorage.getItem("Table");
  alert('yo');
});
});
$(文档).ready(函数(){
var计数=1;
var table=“你搞砸了哟”;
$('#添加')。单击(函数addRow(){
var model=$('#model').val();
var size=$('#size').val();
var resolution=$('#resolution').val();
var backlight=$('#backlight').val();
var pins=$('#pins').val();
var touch=$('#touch').val();
$(“#屏幕”)。追加(“+计数+”+型号+”+大小+“+分辨率+”+背光+“+管脚+”
“+触摸+”);
var key=“table”+计数;
计数++;
});
$(“#删除”)。单击(函数removeRow(){
var row=$('#row').val();
$('table#screens tr#row'+row).remove();
});
$('#save')。单击(函数save(){
var table=$('#screens').html;
setItem(“Table”,Table);
警惕(‘yo’);
});
$(“#打开”)。单击(函数打开(){
$('#screens').html=localStorage.getItem(“表”);
警惕(‘yo’);
});
});

解决这一问题的最佳方法是使用JSON存储表值,并将该结构存储在本地存储或数据库中。但是为了回答您的问题,我认为这就是从localStorage存储和检索表结构所需要的。希望这是你需要的

<!DOCTYPE html>
<html>
<body>

<div id="tablediv"></div>

<script>
// Check browser support
if (typeof(Storage) !== "undefined") {
    // Store
    localStorage.setItem("Mytable", "<table><tr><td>col1</td></tr></table>");
    // Retrieve
    document.getElementById("tableDiv").innerHTML = localStorage.getItem("Mytable");
} else {
    document.getElementById("tableDiv").innerHTML = "Your browser does not support Web Storage";
}
</script>

</body>
</html>

//检查浏览器支持
if(类型(存储)!=“未定义”){
//贮藏
setItem(“Mytable”、“col1”);
//取回
document.getElementById(“tableDiv”).innerHTML=localStorage.getItem(“Mytable”);
}否则{
document.getElementById(“tableDiv”).innerHTML=“您的浏览器不支持Web存储”;
}

你的想法是对的。请确保使用JSON.stringify()和JSON.parse()如下:

var tableData = [{name: "John", email: "jhenry@example.com"}];
// Persist
localStorage.setItem("nameForYourData", JSON.stringify(tableData));
// Retrieve
tableData = JSON.parse(localStorage.getItem("nameForYourData"));

我认为最简单的方法是:

var table = $('table').wrap('<table/>').parent().html();
table = JSON.stringify(table);
window.localStorage.setItem('savedTable', table);

// then later
table = JSON.parse(window.localStorage.getItem('savedTable'))
var table=$('table').wrap(''.parent().html();
table=JSON.stringify(table);
window.localStorage.setItem('savedTable',table);
//后来
table=JSON.parse(window.localStorage.getItem('savedTable'))

这里有一个难题:

我能不能不使用var tableData=$(#myTable).html;而对于第一行,它将存储html,而不是数据。