{"id":3184,"date":"2024-11-10T17:11:46","date_gmt":"2024-11-10T09:11:46","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3184\/"},"modified":"2024-11-10T17:11:46","modified_gmt":"2024-11-10T09:11:46","slug":"js%e5%a6%82%e4%bd%95%e7%bb%99%e6%a0%87%e7%ad%be%e8%b5%8b%e5%80%bc","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3184\/","title":{"rendered":"js\u5982\u4f55\u7ed9\u6807\u7b7e\u8d4b\u503c"},"content":{"rendered":"<blockquote><p>\n  \u5982\u4f55\u4f7f\u7528 javascript \u7ed9\u6807\u7b7e\u8d4b\u503c\uff1a\u83b7\u53d6\u6807\u7b7e\u5f15\u7528\uff1a\u4f7f\u7528 document.queryselector() \u6216 document.getelementbyid() \u65b9\u6cd5\u3002\u8bbe\u7f6e\u6807\u7b7e\u7684 innertext \u6216 innerhtml\uff1a\u8bbe\u7f6e\u6807\u7b7e\u7684\u6587\u672c\u6216 html \u5185\u5bb9\u3002\u8bbe\u7f6e\u6807\u7b7e\u7684 value \u5c5e\u6027\uff1a\u5bf9\u4e8e\u8f93\u5165\u5143\u7d20\uff0c\u4f7f\u7528 value \u5c5e\u6027\u8bbe\u7f6e\u5176\u503c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110806281687379.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u7ed9\u6807\u7b7e\u8d4b\u503c\u63d2\u56fe\" alt=\"js\u5982\u4f55\u7ed9\u6807\u7b7e\u8d4b\u503c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 JavaScript \u7ed9\u6807\u7b7e\u8d4b\u503c<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\uff0c\u7ed9\u6807\u7b7e\u8d4b\u503c\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a<\/p>\n<ol>\n<li><strong>\u83b7\u53d6\u6807\u7b7e\u7684\u5f15\u7528<\/strong><\/li>\n<\/ol>\n<p>\u53ef\u4ee5\u4f7f\u7528 document.querySelector() \u6216 document.getElementById() \u65b9\u6cd5\u6765\u83b7\u53d6\u6807\u7b7e\u7684\u5f15\u7528\u3002<\/p>\n<pre>const label = document.querySelector('label'); \/\/ \u901a\u8fc7\u9009\u62e9\u5668\u83b7\u53d6\u6807\u7b7e\nconst label2 = document.getElementById('myLabel'); \/\/ \u901a\u8fc7 ID \u83b7\u53d6\u6807\u7b7e<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li><strong>\u8bbe\u7f6e\u6807\u7b7e\u7684 innerText \u6216 innerHTML<\/strong><\/li>\n<\/ol>\n<p>innerText \u548c innerHTML \u5c5e\u6027\u5206\u522b\u7528\u4e8e\u8bbe\u7f6e\u6807\u7b7e\u7684\u6587\u672c\u5185\u5bb9\u548c HTML \u5185\u5bb9\u3002<\/p>\n<pre>label.innerText = '\u65b0\u6587\u672c\u5185\u5bb9'; \/\/ \u8bbe\u7f6e\u6807\u7b7e\u7684\u6587\u672c\u5185\u5bb9\nlabel2.innerHTML = '&lt;b&gt;\u52a0\u7c97\u6587\u672c&lt;\/b&gt;'; \/\/ \u8bbe\u7f6e\u6807\u7b7e\u7684 HTML \u5185\u5bb9<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li><strong>\u8bbe\u7f6e\u6807\u7b7e\u7684 value \u5c5e\u6027<\/strong><\/li>\n<\/ol>\n<p>\u5bf9\u4e8e\u8f93\u5165\u5143\u7d20\uff08\u5982  \u6216 \uff09\uff0c\u53ef\u4ee5\u4f7f\u7528 value \u5c5e\u6027\u8bbe\u7f6e\u5176\u503c\u3002<\/p>\n<pre>const input = document.querySelector('input');\ninput.value = '\u8f93\u5165\u503c'; \/\/ \u8bbe\u7f6e\u8f93\u5165\u5143\u7d20\u7684\u503c<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>const label = document.querySelector('label');\nlabel.innerText = '\u65b0\u7684\u6807\u7b7e\u6807\u9898';\nconst input = document.querySelector('input');\ninput.value = '\u8f93\u5165\u7684\u6587\u672c';<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u4f7f\u7528 JavaScript \u7ed9\u6807\u7b7e\u8d4b\u503c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u7ed9\u6807\u7b7e\u8d4b\u503c\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>\u5982\u4f55\u4f7f\u7528 javascript \u7ed9\u6807\u7b7e\u8d4b\u503c\uff1a\u83b7\u53d6\u6807\u7b7e\u5f15\u7528\uff1a\u4f7f\u7528 document.queryselector() \u6216 document.getelementbyid() \u65b9\u6cd5\u3002\u8bbe\u7f6e\u6807\u7b7e\u7684 innertext \u6216 innerhtml\uff1a\u8bbe\u7f6e\u6807\u7b7e\u7684\u6587\u672c\u6216 html \u5185\u5bb9\u3002\u8bbe\u7f6e\u6807\u7b7e\u7684 value \u5c5e\u6027\uff1a\u5bf9\u4e8e\u8f93\u5165\u5143\u7d20\uff0c\u4f7f\u7528 value \u5c5e\u6027\u8bbe\u7f6e\u5176\u503c\u3002 \u5982\u4f55\u4f7f\u7528 JavaScript \u7ed9\u6807\u7b7e\u8d4b\u503c \u5728 JavaScript \u4e2d\uff0c\u7ed9\u6807\u7b7e\u8d4b\u503c\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a \u83b7\u53d6\u6807\u7b7e\u7684\u5f15\u7528 \u53ef\u4ee5\u4f7f\u7528 document.querySelector() \u6216 document.getElementById() \u65b9\u6cd5\u6765\u83b7\u53d6\u6807\u7b7e\u7684\u5f15\u7528\u3002 const label = document.querySelector(&#8216;label&#8217;); \/\/ \u901a\u8fc7\u9009\u62e9\u5668\u83b7\u53d6\u6807\u7b7e const label2 = document.getElementById(&#8216;myLabel&#8217;); \/\/ \u901a\u8fc7 ID \u83b7\u53d6\u6807\u7b7e \u767b\u5f55\u540e\u590d\u5236 \u8bbe\u7f6e\u6807\u7b7e\u7684 innerText \u6216 innerHTML innerText \u548c innerHTML \u5c5e\u6027\u5206\u522b\u7528\u4e8e\u8bbe\u7f6e\u6807\u7b7e\u7684\u6587\u672c\u5185\u5bb9\u548c HTML \u5185\u5bb9\u3002 label.innerText = [&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-3184","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3184","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=3184"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3184\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}