How do we store a string in localStorage?

后端 未结 4 1256
梦如初夏
梦如初夏 2021-01-13 19:14

I\'m learning javascript, xml, and html. For a homework assignment, I need to retrieve some data from some nodes of a XML file, concatenate the data, and store the concaten

相关标签:
4条回答
  • 2021-01-13 19:47

    localStorage.setItem('your_data_key', 'your_data')

    0 讨论(0)
  • 2021-01-13 19:48

    I can't see anywhere in your code where you're storing something to localStorage. You're trying to retrieve localStorage[students], but there's nothing there.

    Note also that you can only store strings in localStorage. You're assigning a jQuery result to students at one point. Were you to try and store that in localStorage it would fail as it's an array of jQuery obects.

    0 讨论(0)
  • 2021-01-13 19:49

    You are not setting the data, you are getting the data that doesn't exist

    I believe what you want is this

    (You have to give a name to the variable in local storage, I'm giving it the name of students)

    localStorage['students'] = students;
    $("#clickme").text("Students' first names: " + students);
    

    After this you can have your get if you want similar to what you had before

    var s_data = localStorage['students'];
    
    0 讨论(0)
  • 2021-01-13 19:54

    You can use the setItem() to set a value to the localStorage and getItem() to retrieve it

    Ex:

    localStorage.setItem('mykey', 'somevalue')
    

    Demo: Fiddle

    0 讨论(0)
提交回复
热议问题