Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript localStorage.setItem可以';t存储html字符串_Javascript_Jquery_String_Html_Local Storage - Fatal编程技术网

Javascript localStorage.setItem可以';t存储html字符串

Javascript localStorage.setItem可以';t存储html字符串,javascript,jquery,string,html,local-storage,Javascript,Jquery,String,Html,Local Storage,我曾经有一个脚本,它使用GM_setValue()存储一个页面中的html块,并将其加载到另一个页面上。 因此: 现在的问题是,它不再像GM-equivs那样存储HTML字符串。新函数就位后,示例脚本将发出警报失败 我尝试过使用正则表达式进行转义,也尝试过使用escape()进行转义,但我没有运气 有人知道该怎么做吗 编辑:尴尬的是,这实际上似乎效果不错。只是不在我需要的范围内。我有一个文本框,它在提交测试时被简单地作为javascript求值,当我从那里调用函数时,它可以工作,但是在代码调用时

我曾经有一个脚本,它使用
GM_setValue()
存储一个页面中的html块,并将其加载到另一个页面上。 因此:

现在的问题是,它不再像GM-equivs那样存储HTML字符串。新函数就位后,示例脚本将发出警报失败

我尝试过使用正则表达式进行转义,也尝试过使用
escape()
进行转义,但我没有运气

有人知道该怎么做吗

编辑:尴尬的是,这实际上似乎效果不错。只是不在我需要的范围内。我有一个文本框,它在提交测试时被简单地作为javascript求值,当我从那里调用函数时,它可以工作,但是在代码调用时抛出一个错误。
谢谢你的帮助,但这并不像我想象的那样是个问题,对不起。

试着像这样转义HTML

var html = '<div id=\"test\">testing</div>';

localStorage.setItem("Foo", html);

刚刚在Chrome上试用过,效果很好。

使用encodeURI存储,然后使用decodeURI检索

localStorage.setItem('html',encodeURI('<div id="test">testing</div>'))
localStorage.getItem('html')
"%3Cdiv%20id=%22test%22%3Etesting%3C/div%3E"
decodeURI(localStorage.getItem('html'))
  "<div id="test">testing</div>"
localStorage.setItem('html',encodeURI('testing'))
localStorage.getItem('html')
%3Cdiv%20id=%22测试%22%3测试%3C/div%3E
decodeURI(localStorage.getItem('html'))
“测试”
我将通过来回转换来实现通用数据的存储。这适用于支持转换为JSON的所有内容(字符串、数字、大多数对象、布尔值等)

以你为例

var data = '<div id="test">testing</div>';
Mem.set('test', data);
console.log(Mem.get('test') === data); // true
Mem.del('test'); // cleanup
var数据='测试';
记忆集(“测试”,数据);
console.log(Mem.get('test')==数据);//真的
Mem.del(‘测试’);//清理

无法复制
var s='testing';sessionStorage.setItem('foo',s);s===sessionStorage.getItem('foo');//是的
他使用的是本地存储而不是会话storage@VoronoiPotato这两种情况对我来说都是一样的。给我一分钟,我会做一把小提琴。我知道,那是一个匆忙中的打字错误。我已经修好了。
var html = '<div id="test">testing</div>';

localStorage.setItem("Foo", html);

console.log(localStorage.getItem("Foo"));
localStorage.setItem('html',encodeURI('<div id="test">testing</div>'))
localStorage.getItem('html')
"%3Cdiv%20id=%22test%22%3Etesting%3C/div%3E"
decodeURI(localStorage.getItem('html'))
  "<div id="test">testing</div>"
var Mem = (function (localStorage, JSON) {
    return {
        'set': function (key, value) {
            return localStorage.setItem(key, JSON.stringify(value));
        },
        'get': function (key) {
            return JSON.parse(localStorage.getItem(key));
        },
        'del': function () {
            for (var i = 0; i < arguments.length; ++i)
                localStorage.removeItem(arguments[i]);
        }
    };
}(window.localStorage, window.JSON)); // protect references
Mem.set('foo', 0);
Mem.set('bar', '0');
Mem.set('baz', false);
Mem.get('foo'); // Number 0
Mem.get('bar'); // String "0"
Mem.get('baz'); // Bool false
Mem.del('foo', 'bar', 'baz'); // cleanup
var data = '<div id="test">testing</div>';
Mem.set('test', data);
console.log(Mem.get('test') === data); // true
Mem.del('test'); // cleanup