{"id":47,"date":"2023-12-20T14:21:03","date_gmt":"2023-12-20T06:21:03","guid":{"rendered":"https:\/\/ch.stear.cn\/?p=47"},"modified":"2025-06-15T00:43:50","modified_gmt":"2025-06-14T16:43:50","slug":"%e8%be%93%e5%85%a5%e6%a1%86%e5%88%b7%e6%96%b0%e9%a1%b5%e9%9d%a2%e5%86%85%e5%ae%b9%e8%87%aa%e5%8a%a8%e5%a1%ab%e5%85%85","status":"publish","type":"post","link":"https:\/\/blog.tfseek.top\/index.php\/2023\/12\/20\/%e8%be%93%e5%85%a5%e6%a1%86%e5%88%b7%e6%96%b0%e9%a1%b5%e9%9d%a2%e5%86%85%e5%ae%b9%e8%87%aa%e5%8a%a8%e5%a1%ab%e5%85%85\/","title":{"rendered":"\u8f93\u5165\u6846\u5237\u65b0\u9875\u9762\u5185\u5bb9\u81ea\u52a8\u586b\u5145"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u524d\u8a00<\/h2>\n\n\n\n<p>\u6709\u7684\u65f6\u5019\u6211\u9700\u8981\u5199\u4e00\u4e2a\u6309\u94ae\uff0c\u5e76\u4e14\u8fd9\u4e2a\u6309\u94ae\u5728\u5237\u65b0\u9875\u9762\u7684\u65f6\u5019\u8bb0\u5f55\u4e0a\u6b21\u7684\u5185\u5bb9\uff0c\u81ea\u52a8\u590d\u539f\uff0c\u6bcf\u6b21\u90fd\u9700\u8981\u8fd9\u6837\u5199\u4e00\u6b21\u4ee3\u7801\uff0c\u592a\u8fc7\u4e8e\u7e41\u7410\u4e86\uff0c\u6240\u4ee5\u6211\u5199\u4e86\u4e2a\u7b80\u5355\u7684js\u5e93\u83b1\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u9700\u8981\u4f9d\u9760jq\u6267\u884c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ee3\u7801<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Javascript<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>const xn = {\n    ls: {\n        get: function (key) {\n            return JSON.parse(localStorage.getItem(key));\n        },\n        list: function () {\n            return JSON.parse(localStorage.getItem(\"\"));\n        },\n        remove: function (key) {\n            localStorage.removeItem(key);\n        },\n        set: function (key, value) {\n            localStorage.setItem(key, JSON.stringify(value));\n        }\n    }\n}\nconst ls = xn.ls;\n$(document).on(\"input\", \"input&#91;af_path]\", function () {\n    const element = $(this);\n    value = element.val();\n    const &#91;fun, tree, ...path] = element.attr(\"af_path\").split(\/:|&gt;\/).filter(Boolean);\n\n    if (fun == \"ls\") {\n        let localStorageTree = ls.get(tree);\n        localStorageTree = localStorageTree ? localStorageTree : {};\n        let currentObj = localStorageTree;\n        for (let i = 0; i &lt; path.length - 1; i++) {\n            if (!currentObj&#91;path&#91;i]]) {\n                currentObj&#91;path&#91;i]] = {};\n            }\n            currentObj = currentObj&#91;path&#91;i]];\n        }\n        currentObj&#91;path&#91;path.length - 1]] = value;\n        ls.set(tree, localStorageTree);\n    }\n})\n$(document).ready(function () {\n    $(\"input&#91;af_path]\").each(function () {\n        const element = $(this);\n        const &#91;fun, tree, ...path] = element.attr(\"af_path\").split(\/:|&gt;\/).filter(Boolean);\n        if (fun === \"ls\") {\n            let localStorageTree = ls.get(tree);\n            if (localStorageTree) {\n                localStorageTree = localStorageTree;\n                let targetValue = localStorageTree;\n                for (const pathItem of path) {\n                    if (targetValue&#91;pathItem]) {\n                        targetValue = targetValue&#91;pathItem];\n                    } else {\n                        targetValue = null;\n                        break;\n                    }\n                }\n                if (targetValue !== null) {\n                    element.val(targetValue);\n                }\n            }\n        }\n    });\n});<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">HTML<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;input type=\"text\" af_path=\"ls:tree&gt;1&gt;key&gt;test1\"&gt;\n&lt;input type=\"text\" af_path=\"ls:tree&gt;2&gt;key&gt;test2\"&gt;\n&lt;input type=\"text\" af_path=\"ls:tree&gt;2&gt;sdfdsf&gt;test3\"&gt;\n&lt;input type=\"text\" af_path=\"ls:trsadee123&gt;2&gt;sdfdsf&gt;test3\"&gt;\n&lt;input type=\"text\" af_path=\"ls:123&gt;2&gt;sdfdsf&gt;test3\"&gt;\n&lt;input type=\"text\" af_path=\"ls:asd&gt;2&gt;sdfdsf&gt;test3\"&gt;\n&lt;input type=\"text\" af_path=\"ls:tree&gt;4&gt;key&gt;test4\"&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u9884\u89c8<\/h2>\n\n\n\n<div>\n    <input type=\"text\" af_path=\"ls:tree>1>key>test1\">\n    <input type=\"text\" af_path=\"ls:tree>2>key>test2\">\n    <input type=\"text\" af_path=\"ls:tree>2>sdfdsf>test3\">\n    <input type=\"text\" af_path=\"ls:trsadee123>2>sdfdsf>test3\">\n    <input type=\"text\" af_path=\"ls:123>2>sdfdsf>test3\">\n    <input type=\"text\" af_path=\"ls:asd>2>sdfdsf>test3\">\n    <input type=\"text\" af_path=\"ls:tree>4>key>test4\">\n<\/div>\n<script src=\"http:\/\/libs.baidu.com\/jquery\/2.0.0\/jquery.min.js\"><\/script>\n<script>\n    const xn = {\n        ls: {\n            get: function (key) {\n                return JSON.parse(localStorage.getItem(key));\n            },\n            list: function () {\n                return JSON.parse(localStorage.getItem(\"\"));\n            },\n            remove: function (key) {\n                localStorage.removeItem(key);\n            },\n            set: function (key, value) {\n                localStorage.setItem(key, JSON.stringify(value));\n            }\n        }\n    }\n    const ls = xn.ls;\n    $(document).on(\"input\", \"input[af_path]\", function () {\n        const element = $(this);\n        value = element.val();\n        const [fun, tree, ...path] = element.attr(\"af_path\").split(\/:|>\/).filter(Boolean);\n\n        if (fun == \"ls\") {\n            let localStorageTree = ls.get(tree);\n            localStorageTree = localStorageTree ? localStorageTree : {};\n            let currentObj = localStorageTree;\n            for (let i = 0; i < path.length - 1; i++) {\n                if (!currentObj[path[i]]) {\n                    currentObj[path[i]] = {};\n                }\n                currentObj = currentObj[path[i]];\n            }\n            currentObj[path[path.length - 1]] = value;\n            ls.set(tree, localStorageTree);\n        }\n    })\n    $(document).ready(function () {\n        $(\"input[af_path]\").each(function () {\n            const element = $(this);\n            const [fun, tree, ...path] = element.attr(\"af_path\").split(\/:|>\/).filter(Boolean);\n            if (fun === \"ls\") {\n                let localStorageTree = ls.get(tree);\n                if (localStorageTree) {\n                    localStorageTree = localStorageTree;\n                    let targetValue = localStorageTree;\n                    for (const pathItem of path) {\n                        if (targetValue[pathItem]) {\n                            targetValue = targetValue[pathItem];\n                        } else {\n                            targetValue = null;\n                            break;\n                        }\n                    }\n                    if (targetValue !== null) {\n                        element.val(targetValue);\n                    }\n                }\n            }\n        });\n    });\n<\/script>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u6709\u7684\u65f6\u5019\u6211\u9700\u8981\u5199\u4e00\u4e2a\u6309\u94ae\uff0c\u5e76\u4e14\u8fd9\u4e2a\u6309\u94ae\u5728\u5237\u65b0\u9875\u9762\u7684\u65f6\u5019\u8bb0\u5f55\u4e0a\u6b21\u7684\u5185\u5bb9\uff0c\u81ea\u52a8\u590d\u539f\uff0c\u6bcf\u6b21\u90fd\u9700\u8981\u8fd9\u6837\u5199\u4e00\u6b21\u4ee3\u7801\uff0c\u592a\u8fc7\u4e8e\u7e41\u7410\u4e86\uff0c\u6240\u4ee5 &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[3,5],"class_list":["post-47","post","type-post","status-publish","format-standard","hentry","category-4","tag-3","tag-5"],"_links":{"self":[{"href":"https:\/\/blog.tfseek.top\/index.php\/wp-json\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.tfseek.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.tfseek.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.tfseek.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.tfseek.top\/index.php\/wp-json\/wp\/v2\/comments?post=47"}],"version-history":[{"count":1,"href":"https:\/\/blog.tfseek.top\/index.php\/wp-json\/wp\/v2\/posts\/47\/revisions"}],"predecessor-version":[{"id":91,"href":"https:\/\/blog.tfseek.top\/index.php\/wp-json\/wp\/v2\/posts\/47\/revisions\/91"}],"wp:attachment":[{"href":"https:\/\/blog.tfseek.top\/index.php\/wp-json\/wp\/v2\/media?parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.tfseek.top\/index.php\/wp-json\/wp\/v2\/categories?post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.tfseek.top\/index.php\/wp-json\/wp\/v2\/tags?post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}