javascript - 使用javascript将contentEditable保存到html文件中

标签 javascript edit

如何使用 javascript(无 PHP)将 contenteditable 元素保存到实际的 HTML 代码中?因此,即使在离线模式下,我也可以随时编辑内容。 就像当您单击“保存按钮”时,它会用新文件替换旧文件(有更改的文本)。

如果有任何其他编程语言可以使它在离线模式下工作的方法,请提出建议。

我找到了几个示例,但它们都是用 PHP 制作的。

另外,我会贴出代码。在此代码中,您可以使用 javascript 编辑文件并保存。但问题是它不会保存到实际的 HTML 代码中。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
</head>
<style type="text/css">
 body{ 
  font-family: "Dosis"; 
  font-size: 1.3em;
  line-height: 1.6em;
}

.headline{
  font-size: 2em;
  text-align: center;
}

#wrapper {
  width: 600px;
  background: #FFF;
  padding: 1em;
  margin: 1em auto;
  box-shadow: 0 2px 5px rgba(0,0,0,0.3);
  border-radius: 3px;
}

button {
  border: none;
  padding: 0.8em;
  background: #F96;
  border-radius: 3px;
  color: white;
  font-weight: bold;
  margin: 0 0 1em;
}

button:hover, button:focus {
  cursor: pointer;
  outline: none;
}

#editor {
  padding: 1em;
  background: #E6E6E6;
  border-radius: 3px;
}

</style>
<body>

<div id="wrapper">

  <section>
    <h1 class="headline">contentEditable Demonstration</h1>

    <button id="editBtn" type="button">Edit Document</button>
    <div id="editDocument">
      <h1 id="title">A Nice Heading.</h1>
      <p>Last Edited by <span id="author">Monty Shokeen</span>
      </p>
      <p id="content">You can change the heading, author name and this content itself. Click on Edit Document to start editing. At this point, you can edit this document and the changes will be saved in localStorage. However, once you reload the page your changes will be gone. To fix it we will have to retrieve the contents from localSotrage when the page reloads.</p>
    </div>
  </section>
</div>

    <script>
    var editBtn = document.getElementById('editBtn');
var editables = document.querySelectorAll('#title, #author, #content');

if (typeof(Storage) !== "undefined") {
  if (localStorage.getItem('title') !== null) {
    editables[0].innerHTML = localStorage.getItem('title');
  }
  if (localStorage.getItem('author') !== null) {
    editables[1].innerHTML = localStorage.getItem('author');
  }
  if (localStorage.getItem('content') !== null) {
    editables[2].innerHTML = localStorage.getItem('content');
  }
}

editBtn.addEventListener('click', function(e) {
  if (!editables[0].isContentEditable) {
    editables[0].contentEditable = 'true';
    editables[1].contentEditable = 'true';
    editables[2].contentEditable = 'true';
    editBtn.innerHTML = 'Save Changes';
    editBtn.style.backgroundColor = '#6F9';
  } else {
    // Disable Editing
    editables[0].contentEditable = 'false';
    editables[1].contentEditable = 'false';
    editables[2].contentEditable = 'false';
    // Change Button Text and Color
    editBtn.innerHTML = 'Enable Editing';
    editBtn.style.backgroundColor = '#F96';
    // Save the data in localStorage 
    for (var i = 0; i < editables.length; i++) {
      localStorage.setItem(editables[i].getAttribute('id'), editables[i].innerHTML);
    }
  }
});
    </script>
</body>

</html>

最佳答案

您需要使用类似 downloadInnerHtml 函数的功能,如 here 所述.理想情况下,您可能还希望在导出之前删除脚本标签和内容可编辑属性,因为您不希望最终的 html 页面是可编辑的

关于javascript - 使用javascript将contentEditable保存到html文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53066185/

相关文章:

javascript - Chrome 扩展 : Getting the document. images.length 来自当前页面,而不是弹出窗口。 (Javascript)

javascript - 无法解析模块说明符 "vee-validate"。相对引用必须以 "/"、 "./"或 "../"开头

javascript - 如何检查 Protractor 中是否存在编辑后的结果?

C++ 在 .txt 文件中查找整数值

json - 如何在基于字段值替换项目的同时在 jq 中输出整个文档?

javascript - 用户做了某事但停留在同一页面

javascript - 隐藏 <canvas> 元素

r - R函数edit()经常崩溃而无法自发

java - 用于编辑 ArrayList 中对象的 boolean 方法

javascript - 如何阻止 Google Chrome 重组长数组?