简体   繁体   English

如何使用 jQuery 将 span 标记添加到选定文本并将更改永久保存在本地 html 文件中?

[英]How to add span tag to a selected text using jQuery and saving the change permanently in local html file?

I have created notes in html file locally and intend to use the file only locally in my computer.我在本地创建了 html 文件中的注释,并打算仅在我的计算机本地使用该文件。 Let's say i have a code假设我有一个代码

<p> this is an example text</p>

I want to select the word example in the browser and preferably press a shortcut to change the code as我想在浏览器中选择单词示例,最好按快捷方式将代码更改为

<p> this is an <span class="highlight">example</span> text</p>

and save this change in the same html file.并将此更改保存在同一个 html 文件中。

Especially, I have no clue on how to approach saving the changes in the same local html file.特别是,我不知道如何在同一个本地 html 文件中保存更改。

Edit: I intend to use this notes only for personal use and will be only locally stored in my computer as a .html file.编辑:我打算将此笔记仅用于个人用途,并将仅作为 .html 文件本地存储在我的计算机中。

Edit2: Can this be achieved using php? Edit2:这可以使用php来实现吗?

but I have no clue on how to approach saving the changes in the same local html file.但我不知道如何在同一个本地 html 文件中保存更改。

You can't change the source code of your HTML/JS/CSS file from browser.您无法从浏览器更改 HTML/JS/CSS 文件的源代码。 It posses a serious security risk to your application and hence will be prohibited by the browser.它对您的应用程序构成严重的安全风险,因此将被浏览器禁止。

If you want to remember that you have made this change, then save a value in either localstorage or cookie so that your program read this value at startup and make the change in your markup without a user event.如果您想记住您进行了此更改,请在 localstorage 或 cookie 中保存一个值,以便您的程序在启动时读取此值,并在没有用户事件的情况下在您的标记中进行更改。

You can't write to the file system using HTML only - you can however use JS to generate an AJAX request to a server side script (ie PHP/Node/etc.) which in turn writes to a file.您不能仅使用 HTML 写入文件系统 - 但是您可以使用 JS 生成对服务器端脚本(即 PHP/Node/等)的 AJAX 请求,该请求又写入文件。

You could retrieve most of the contents by accessing document.body.parentNode.innerHTML changes you've made to DOM since loading the page should be reflected here.您可以通过访问您对 DOM 所做的document.body.parentNode.innerHTML更改来检索大部分内容,因为加载页面应该反映在此处。 It's worth noting that this attribute will not include the HTML tag, or the DTD - it will only include the head and body tags so you'll need to be sure to add them in your server side script.值得注意的是,此属性不包含 HTML 标记或 DTD - 它仅包含 head 和 body 标记,因此您需要确保将它们添加到服务器端脚本中。

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

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