简体   繁体   中英

How to save/persist and access data on client side in javaScript:Jquery?

How to save/persist and access data on client side in javaScript:Jquery?

I am using jsTree component of Jquery:JavaScript.

In bind() of jstree, after selection of node I want to save "data"

.bind("select_node.jstree", function (event, data) { 
// where & how to save data ?
}

$("#Btn").click(function() {
// I want to access data value on click event
}

I tried to set data object in cookie and accessed same in $("#Btn").click(function() {... But can't access same data object

+

I also tried jQuery.data() ie In bind() set data like....

jQuery.data(div, "selectedNode", data);

and tried to access in $("#Btn").click(function(){.... like

var selectedNodeData = jQuery.data(div, "selectedNode"); // Can't access

Any help or guidance in this matter would be appreciated.

您的解决方案是Web存储,它是HTML 5 流行语的一部分,但较旧的浏览器不支持此存储,您可以使用localstorage

To persist datas across multiple pages , there are the localStorage , sessionStorage and the cookies solutions.

If you haven't got it working, then you got something wrong. I'd suggest you show us the code you used for your cookies :)

To share datas on a single page , you can use the data() in jQuery. But this way, you attach datas to a DOM element, not nowhere. Here is a sample:

$.data( '#myId', 'selectedNode', '#nodeSelected' )
// There, I attach the "selectedNode" key with the "#nodeSelected" value
// to the "#myId" element.

// To retrieve the data later, you have to use:
var el = $.data( '#myId', 'selectedNode' )

// Then, you can use the value like this:
$( el ).on( 'click', fn )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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