简体   繁体   English

使用javascript或jquery从保存的html中获取与元素相关的jquery.data()

[英]get jquery.data() related to an element from a saved html using javascript or jquery

I have some html that I want to save in database for a later retrieve. 我有一些要保存在数据库中以供以后检索的html。

Let's imagine the html is a simple div 假设html是一个简单的div

<div id="mydiv">This is my div</div>

I use jQuery.data() to store some information related to that div like this : 我使用jQuery.data()来存储一些与该div相关的信息,如下所示:

$("#mydiv").data("divNumber", "5").data("divRole", "adminMessage") .....

Then finally I save the html in database, but I would like to be able to get those information later when I need them : 然后最后我将html保存在数据库中,但是我希望以后可以在需要它们时获得这些信息:

var myHtml = { here I get the html from my database }

$("body").append(myHtml);

console.log( $("#mydiv").data("divNumber") ); // I want it to show 5

console.log( $("#mydiv").data("divRole") ); // I want it to show adminMessag

from my understanding of jquery.data() I think it will just store those data information temporary in an internal cache and if I save the html in the database, and I leave the page, they will be lost !! 从我对jquery.data()的理解来看,我认为它将只是将那些数据信息临时存储在内部缓存中,如果我将html保存在数据库中,然后离开页面,它们将会丢失!

So is there a way to keep those jquery.data() information and retrieve them whenever I want ? 那么有没有一种方法可以保留那些jquery.data()信息并在需要时检索它们? or maybe there is another "better" way to achieve the same thing (I am not looking for localStorage method) 也许还有另一种“更好”的方法可以实现相同的目的(我不是在寻找localStorage方法)

You can convert the .data() to data attributes on the html and save that. 您可以将.data()转换为html上的数据属性,然后保存。

 $("#mydiv").data("divNumber", "5").data("divRole", "adminMessage"); $.each($("#mydiv").data(), function(k,v){ $("#mydiv").attr("data-"+k.replace(/[AZ]/g, "-$&"), v); }); var toDatabase = $("#mydiv")[0].outerHTML; //save to database // ... // retrieve from database var fromDatabase = $(toDatabase); $('body').append(fromDatabase.data('divNumber')); $('body').append('<br>'); $('body').append(fromDatabase.data('divRole')); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="mydiv">This is my div</div> 

If you want to have a database in the browser which works across all platforms consistently, despite each browser having different underlying storage mechanisms try PouchDB. 如果您希望浏览器中的数据库能够在所有平台上保持一致,尽管每个浏览器具有不同的基础存储机制,请尝试使用PouchDB。 It is great! 这太棒了! You can keep it just in your browser session which will persist until the user clears their data OR you can have it persist to a CouchDB database server. 您可以将其保留在浏览器会话中,该会话将一直保留到用户清除其数据为止,或者可以将其保留到CouchDB数据库服务器中。

http://pouchdb.com/ http://pouchdb.com/

There are many good CouchDB providers if you dont want to setup your own although that is simple to do. 有很多不错的CouchDB提供程序,如果您不想自己设置,尽管这样做很简单。 IBM Cloudant is free until your usage is above $50 per month: 在您每月的使用费超过$ 50之前,IBM Cloudant是免费的:

https://cloudant.com/ https://cloudant.com/

It is very simple to use, see here . 使用非常简单,请参阅此处 A couple lines of code to create a db object and a few to put / fetch. 几行代码创建一个db对象,几行代码放置/获取。 Then one line each to setup a remote copy and one line to live sync to it. 然后每行一行以设置一个远程副本,另一行进行实时同步。

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

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