{"id":3215,"date":"2024-11-10T14:13:27","date_gmt":"2024-11-10T06:13:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3215\/"},"modified":"2024-11-10T14:13:27","modified_gmt":"2024-11-10T06:13:27","slug":"js%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e5%9b%be%e7%89%87%e7%9a%84%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3215\/","title":{"rendered":"js\u5982\u4f55\u83b7\u53d6\u56fe\u7247\u7684\u8def\u5f84"},"content":{"rendered":"<blockquote><p>\n  javascript \u4e2d\u83b7\u53d6\u56fe\u50cf\u8def\u5f84\u6709\u4e09\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a\u4f7f\u7528 src \u5c5e\u6027\u3001\u4f7f\u7528 getattribute() \u65b9\u6cd5\u3001\u4f7f\u7528 url.createobjecturl() \u65b9\u6cd5\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0csrc \u5c5e\u6027\u548c getattribute() \u65b9\u6cd5\u90fd\u8fd4\u56de\u7edd\u5bf9\u8def\u5f84\uff0c\u4f46\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u6216\u7b2c\u4e8c\u4e2a\u53c2\u6570 &#8220;src&#8221; \u6765\u83b7\u53d6\u76f8\u5bf9\u8def\u5f84\u3002\u5982\u679c\u56fe\u50cf\u6258\u7ba1\u5728\u5176\u4ed6\u57df\uff0c\u53ef\u80fd\u4f1a\u9047\u5230 cors \u9650\u5236\uff0c\u53ef\u4ee5\u4f7f\u7528 cors \u4ee3\u7406\u3001cors \u5934\u6216 jsonp \u6765\u89e3\u51b3\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110807190764605.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u83b7\u53d6\u56fe\u7247\u7684\u8def\u5f84\u63d2\u56fe\" alt=\"js\u5982\u4f55\u83b7\u53d6\u56fe\u7247\u7684\u8def\u5f84\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 JavaScript \u83b7\u53d6\u56fe\u7247\u8def\u5f84<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\uff0c\u83b7\u53d6\u56fe\u50cf\u8def\u5f84\u6709\u4e09\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 src \u5c5e\u6027<\/strong><\/p>\n<p>\u8fd9\u662f\u83b7\u53d6\u56fe\u50cf\u7edd\u5bf9\u8def\u5f84\u7684\u6700\u7b80\u5355\u65b9\u6cd5\uff1a<\/p>\n<pre>const imageUrl = document.querySelector('img').src;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. \u4f7f\u7528 getAttribute() \u65b9\u6cd5<\/strong><\/p>\n<p>\u8fd9\u5141\u8bb8\u4f60\u83b7\u53d6\u56fe\u50cf\u7684\u76f8\u5bf9\u8def\u5f84\u6216\u7edd\u5bf9\u8def\u5f84\uff1a<\/p>\n<pre>const imageUrl = document.querySelector('img').getAttribute('src');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u4f7f\u7528 URL.createObjectURL() \u65b9\u6cd5<\/strong><\/p>\n<p>\u6b64\u65b9\u6cd5\u4ec5\u9002\u7528\u4e8e\u5728\u5185\u5b58\u4e2d\u5b58\u50a8\u7684\u56fe\u50cf\uff0c\u4f8b\u5982\u4ece\u753b\u5e03\u6216\u76f8\u673a\u6355\u83b7\u7684\u56fe\u50cf\uff1a<\/p>\n<pre>const imageUrl = URL.createObjectURL(imageFile);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u83b7\u53d6\u76f8\u5bf9\u8def\u5f84\u6216\u7edd\u5bf9\u8def\u5f84<\/strong><\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0csrc \u5c5e\u6027\u548c getAttribute() \u65b9\u6cd5\u90fd\u8fd4\u56de\u56fe\u50cf\u7684\u7edd\u5bf9\u8def\u5f84\u3002\u4f46\u4f60\u53ef\u4ee5\u901a\u8fc7\u5728 src \u5c5e\u6027\u4e2d\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u6216\u5728 getAttribute() \u65b9\u6cd5\u4e2d\u4f7f\u7528\u7b2c\u4e8c\u4e2a\u53c2\u6570 &#8220;src&#8221; \u6765\u83b7\u53d6\u76f8\u5bf9\u8def\u5f84\uff1a<\/p>\n<pre>\/\/ \u83b7\u53d6\u76f8\u5bf9\u8def\u5f84\nconst relativeImageUrl = document.querySelector('img').getAttribute('src', \"src\");<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u5904\u7406 CORS \u9650\u5236<\/strong><\/p>\n<p>\u5982\u679c\u56fe\u50cf\u6258\u7ba1\u5728\u5176\u4ed6\u57df\uff0c\u5219\u53ef\u80fd\u4f1a\u9047\u5230\u8de8\u57df\u8d44\u6e90\u5171\u4eab (CORS) \u9650\u5236\u3002\u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6280\u672f\u4e4b\u4e00\uff1a<\/p>\n<ul>\n<li> <strong>CORS \u4ee3\u7406\uff1a<\/strong>\u5728\u670d\u52a1\u5668\u7aef\u8bbe\u7f6e\u4ee3\u7406\u4ee5\u8f6c\u53d1\u8bf7\u6c42\u3002<\/li>\n<li> <strong>CORS \u5934\uff1a<\/strong>\u6388\u6743\u8de8\u57df\u8bf7\u6c42\u7684\u54cd\u5e94\u6807\u5934\u3002<\/li>\n<li> <strong>JSONP\uff1a<\/strong>\u4e00\u79cd JavaScript \u6280\u672f\uff0c\u5141\u8bb8\u8de8\u57df\u8bf7\u6c42\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u83b7\u53d6\u56fe\u7247\u7684\u8def\u5f84\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>javascript \u4e2d\u83b7\u53d6\u56fe\u50cf\u8def\u5f84\u6709\u4e09\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a\u4f7f\u7528 src \u5c5e\u6027\u3001\u4f7f\u7528 getattribute() \u65b9\u6cd5\u3001\u4f7f\u7528 url.createobjecturl() \u65b9\u6cd5\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0csrc \u5c5e\u6027\u548c getattribute() \u65b9\u6cd5\u90fd\u8fd4\u56de\u7edd\u5bf9\u8def\u5f84\uff0c\u4f46\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u6216\u7b2c\u4e8c\u4e2a\u53c2\u6570 &#8220;src&#8221; \u6765\u83b7\u53d6\u76f8\u5bf9\u8def\u5f84\u3002\u5982\u679c\u56fe\u50cf\u6258\u7ba1\u5728\u5176\u4ed6\u57df\uff0c\u53ef\u80fd\u4f1a\u9047\u5230 cors \u9650\u5236\uff0c\u53ef\u4ee5\u4f7f\u7528 cors \u4ee3\u7406\u3001cors \u5934\u6216 jsonp \u6765\u89e3\u51b3\u3002 \u5982\u4f55\u4f7f\u7528 JavaScript \u83b7\u53d6\u56fe\u7247\u8def\u5f84 \u5728 JavaScript \u4e2d\uff0c\u83b7\u53d6\u56fe\u50cf\u8def\u5f84\u6709\u4e09\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 src \u5c5e\u6027 \u8fd9\u662f\u83b7\u53d6\u56fe\u50cf\u7edd\u5bf9\u8def\u5f84\u7684\u6700\u7b80\u5355\u65b9\u6cd5\uff1a const imageUrl = document.querySelector(&#8216;img&#8217;).src; \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 getAttribute() \u65b9\u6cd5 \u8fd9\u5141\u8bb8\u4f60\u83b7\u53d6\u56fe\u50cf\u7684\u76f8\u5bf9\u8def\u5f84\u6216\u7edd\u5bf9\u8def\u5f84\uff1a const imageUrl = document.querySelector(&#8216;img&#8217;).getAttribute(&#8216;src&#8217;); \u767b\u5f55\u540e\u590d\u5236 3. \u4f7f\u7528 URL.createObjectURL() \u65b9\u6cd5 \u6b64\u65b9\u6cd5\u4ec5\u9002\u7528\u4e8e\u5728\u5185\u5b58\u4e2d\u5b58\u50a8\u7684\u56fe\u50cf\uff0c\u4f8b\u5982\u4ece\u753b\u5e03\u6216\u76f8\u673a\u6355\u83b7\u7684\u56fe\u50cf\uff1a const imageUrl = URL.createObjectURL(imageFile); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3215","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3215"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3215\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}