{"id":3143,"date":"2024-11-10T15:03:38","date_gmt":"2024-11-10T07:03:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3143\/"},"modified":"2024-11-10T15:03:38","modified_gmt":"2024-11-10T07:03:38","slug":"js%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3143\/","title":{"rendered":"js\u5982\u4f55\u83b7\u53d6\u5bf9\u8c61"},"content":{"rendered":"<blockquote><p>\n  javascript \u4e2d\u83b7\u53d6\u5bf9\u8c61\u7684\u65b9\u6cd5\u5305\u62ec\uff1a1. \u521b\u5efa\u65b0\u5bf9\u8c61\uff1b2. \u4ece\u73b0\u6709\u5bf9\u8c61\u83b7\u53d6\uff08\u70b9\u8868\u793a\u6cd5\u3001\u65b9\u62ec\u53f7\u8868\u793a\u6cd5\uff09\uff1b3. \u4ece dom \u83b7\u53d6\uff08getelementbyid()\u3001getelementsbytagname()\u3001getelementsbyclassname()\uff09\uff1b4. \u4ece\u6570\u7ec4\u83b7\u53d6\uff08arr[index]\uff09\uff1b5. \u4ece\u51fd\u6570\u83b7\u53d6\uff08functionname()\uff09\uff1b6. \u4ece\u53c2\u6570\u83b7\u53d6\uff08functionname(param)\uff09\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110805031654168.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u83b7\u53d6\u5bf9\u8c61\u63d2\u56fe\" alt=\"js\u5982\u4f55\u83b7\u53d6\u5bf9\u8c61\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u4e2d\u83b7\u53d6\u5bf9\u8c61<\/strong><\/p>\n<p>JavaScript \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u83b7\u53d6\u5bf9\u8c61\uff1a<\/p>\n<p><strong>1. \u521b\u5efa\u65b0\u5bf9\u8c61<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 new \u5173\u952e\u5b57\uff1alet myObject = new Object()<\/li>\n<li>\u4f7f\u7528\u5bf9\u8c61\u5b57\u9762\u91cf\uff1alet myObject = { name: &#8220;John Doe&#8221;, age: 30 }<\/li>\n<\/ul>\n<p><strong>2. \u4ece\u73b0\u6709\u5bf9\u8c61\u83b7\u53d6<\/strong><\/p>\n<ul>\n<li> <strong>\u70b9\u8868\u793a\u6cd5\uff1a<\/strong>myObject.name<\/li>\n<li> <strong>\u65b9\u62ec\u53f7\u8868\u793a\u6cd5\uff1a<\/strong>myObject[&#8220;name&#8221;]<\/li>\n<\/ul>\n<p><strong>3. \u4ece DOM \u83b7\u53d6<\/strong><\/p>\n<ul>\n<li>getElementById()\uff1a\u6839\u636e\u5143\u7d20\u7684 ID \u83b7\u53d6\u5143\u7d20<\/li>\n<li>getElementsByTagName()\uff1a\u6839\u636e\u5143\u7d20\u7684\u6807\u7b7e\u540d\u83b7\u53d6\u5143\u7d20\u5217\u8868<\/li>\n<li>getElementsByClassName()\uff1a\u6839\u636e\u5143\u7d20\u7684\u7c7b\u540d\u83b7\u53d6\u5143\u7d20\u5217\u8868<\/li>\n<\/ul>\n<p><strong>4. \u4ece\u6570\u7ec4\u83b7\u53d6<\/strong><\/p>\n<ul>\n<li>arr[index]\uff1a\u6839\u636e\u7d22\u5f15\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u5143\u7d20<\/li>\n<\/ul>\n<p><strong>5. \u4ece\u51fd\u6570\u83b7\u53d6<\/strong><\/p>\n<ul>\n<li>functionName()\uff1a\u8c03\u7528\u51fd\u6570\u5e76\u8fd4\u56de\u5176\u8fd4\u56de\u503c\uff08\u5982\u679c\u51fd\u6570\u8fd4\u56de\u5bf9\u8c61\uff09<\/li>\n<\/ul>\n<p><strong>6. \u4ece\u53c2\u6570\u83b7\u53d6<\/strong><\/p>\n<ul>\n<li>functionName(param)\uff1a\u5c06\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u4ece\u73b0\u6709\u5bf9\u8c61\u83b7\u53d6\u5c5e\u6027\nlet myObject = { name: \"John Doe\", age: 30 };\nlet myName = myObject.name; \/\/ \"John Doe\"\n\n\/\/ \u4ece DOM \u4e2d\u83b7\u53d6\u5143\u7d20\nlet myElement = document.getElementById(\"myElement\");\n\n\/\/ \u4ece\u6570\u7ec4\u4e2d\u83b7\u53d6\u5143\u7d20\nlet myArray = [\"apple\", \"banana\", \"cherry\"];\nlet myFruit = myArray[0]; \/\/ \"apple\"\n\n\/\/ \u4ece\u51fd\u6570\u4e2d\u83b7\u53d6\u8fd4\u56de\u503c\nfunction getObject() {\n  return { name: \"John Doe\", age: 30 };\n}\nlet myObject = getObject(); \/\/ { name: \"John Doe\", age: 30 }<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u83b7\u53d6\u5bf9\u8c61\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\u5bf9\u8c61\u7684\u65b9\u6cd5\u5305\u62ec\uff1a1. \u521b\u5efa\u65b0\u5bf9\u8c61\uff1b2. \u4ece\u73b0\u6709\u5bf9\u8c61\u83b7\u53d6\uff08\u70b9\u8868\u793a\u6cd5\u3001\u65b9\u62ec\u53f7\u8868\u793a\u6cd5\uff09\uff1b3. \u4ece dom \u83b7\u53d6\uff08getelementbyid()\u3001getelementsbytagname()\u3001getelementsbyclassname()\uff09\uff1b4. \u4ece\u6570\u7ec4\u83b7\u53d6\uff08arr[index]\uff09\uff1b5. \u4ece\u51fd\u6570\u83b7\u53d6\uff08functionname()\uff09\uff1b6. \u4ece\u53c2\u6570\u83b7\u53d6\uff08functionname(param)\uff09\u3002 \u5982\u4f55\u5728 JavaScript \u4e2d\u83b7\u53d6\u5bf9\u8c61 JavaScript \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u83b7\u53d6\u5bf9\u8c61\uff1a 1. \u521b\u5efa\u65b0\u5bf9\u8c61 \u4f7f\u7528 new \u5173\u952e\u5b57\uff1alet myObject = new Object() \u4f7f\u7528\u5bf9\u8c61\u5b57\u9762\u91cf\uff1alet myObject = { name: &#8220;John Doe&#8221;, age: 30 } 2. \u4ece\u73b0\u6709\u5bf9\u8c61\u83b7\u53d6 \u70b9\u8868\u793a\u6cd5\uff1amyObject.name \u65b9\u62ec\u53f7\u8868\u793a\u6cd5\uff1amyObject[&#8220;name&#8221;] 3. \u4ece DOM \u83b7\u53d6 getElementById()\uff1a\u6839\u636e\u5143\u7d20\u7684 ID \u83b7\u53d6\u5143\u7d20 getElementsByTagName()\uff1a\u6839\u636e\u5143\u7d20\u7684\u6807\u7b7e\u540d\u83b7\u53d6\u5143\u7d20\u5217\u8868 getElementsByClassName()\uff1a\u6839\u636e\u5143\u7d20\u7684\u7c7b\u540d\u83b7\u53d6\u5143\u7d20\u5217\u8868 4. \u4ece\u6570\u7ec4\u83b7\u53d6 arr[index]\uff1a\u6839\u636e\u7d22\u5f15\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u5143\u7d20 5. \u4ece\u51fd\u6570\u83b7\u53d6 functionName()\uff1a\u8c03\u7528\u51fd\u6570\u5e76\u8fd4\u56de\u5176\u8fd4\u56de\u503c\uff08\u5982\u679c\u51fd\u6570\u8fd4\u56de\u5bf9\u8c61\uff09 6. \u4ece\u53c2\u6570\u83b7\u53d6 [&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-3143","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3143","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=3143"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3143\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}