将src属性的内容保存在localstorage中

发布于 2024-11-27 21:03:22 字数 94 浏览 0 评论 0原文

我正在开发一个在线操作系统 我想将应用程序图标保存在本地存储中,但我不知道应该如何将 src 属性的内容保存在本地存储中并在应用程序页面上再次读出。我当前的代码会导致空白页。

I am developping an online os
And i want to save the application icons in the localstorage but i don't know how i should save the content of the src attribute in the localstorage and read it out again on the apps page. my current code results in a blank page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

马蹄踏│碎落叶 2024-12-04 21:03:22

您只是想保存源 URL 吗?还是图像内容?

您可以通过以下方式保存源 URL:

var theImage = document.getElementById('my-special-image');
if (window.localStorage) {
     window.localStorage['theImage_src'] = theImage.src;
}

如果您想保存图像的内容,那么您必须对画布进行一些摆弄,使用
canvas.toDataURL()。有关如何执行此操作的更多信息,请参阅这篇文章

Are you simply trying to save the source URL? Or the image content?

You can save source URL by something like the following:

var theImage = document.getElementById('my-special-image');
if (window.localStorage) {
     window.localStorage['theImage_src'] = theImage.src;
}

If you want to save the contents of the image, then you have to do some fiddling around with canvas, using
canvas.toDataURL(). See this post for more info on how to do that.

浅忆流年 2024-12-04 21:03:22

据我所知没有办法做到这一点。您可以保存到本地存储的所有内容都必须由变量引用。当调用 localStorage.setItem 方法时,该值将转换为字符串格式。

localStorage.setItem('my', {a: 1});

console.log(localStorage['my']); // you will get a string value: '[object Object]'

As far as I know there is no way to do it. Everything you could save to local storage must be referenced by a variable. And the value would be converted into string format when call localStorage.setItem method.

localStorage.setItem('my', {a: 1});

console.log(localStorage['my']); // you will get a string value: '[object Object]'
旧伤还要旧人安 2024-12-04 21:03:22

图标是用户提供的还是您自己的?无论哪种情况,都请将应用程序图标转换为 base64。然后将其粘贴到localStorage中。当然,您必须对要存储的数据进行字符串化。

var base64 = localStorage.getItem('icon_name');
$('#imgid').attr('src', base64);

Are the icons user supplied or your own? In either case, convert your application icons to base64. Then stick it into localStorage. Of course you'll have to stringify the data to store.

var base64 = localStorage.getItem('icon_name');
$('#imgid').attr('src', base64);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文