简体   繁体   English

是否可以将DOM操作保存在原始HTML文件中?

[英]Is it possible to save DOM manipulations in original HTML file?

Lets say some part of my webpage is: 可以说我网页的某些部分是:

<p id = "para"></p>

And I perform: 我执行:

 $("#para").text("Hello world!");

Now effectively webpage becomes: 现在有效的网页变为:

<p id = "para">Hello world</p>

Is there a JavaScript or Jquery method which can save such DOM manipulations performed in some part of my webpage so that these manipulations reappear when I open my webpage again? 是否有JavaScriptJquery方法可以保存在网页的某些部分执行的此类DOM操作,以便当我再次打开网页时这些操作重新出现?

In other words, can I write these changed in my HTML file? 换句话说,我可以将这些更改写到HTML文件中吗?

EDIT: In reality I want to save a form, which will grow larger and larger with everyday use. 编辑:实际上,我想保存一个表格,该表格在日常使用中会越来越大。

Yes! 是! You can use localstorage ( https://developer.mozilla.org/en-US/docs/Web/API/Storage/LocalStorage ) to store the changes and you may possibly use mutationobserver ( https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver ) to monitor changes. 您可以使用localstorage( https://developer.mozilla.org/en-US/docs/Web/API/Storage/LocalStorage )来存储更改,并且可能使用mutationobserver( https://developer.mozilla.org/ zh-CN / docs / Web / API / MutationObserver )以监视更改。

For me, what you are looking for is stock the text to get it later. 对我来说,您正在寻找的是库存文字,以备将来使用。 I recommand LocalStorage. 我建议使用LocalStorage。 Like this : 像这样 :

//to stock
localStorage.setItem('myTextVariableName', 'HelloWorld!');

//to load
$("#para").text(localStorage.getItem('myTextVariableName'));

Then you'll get it once you load your page another time 然后,一旦您再次加载页面,就会得到它

plug in js script to html, and code will run every time when you open the html page. 将js脚本插入html,每次打开html页面时代码都会运行。 Also you can use local storage. 您也可以使用本地存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM