{"id":303060,"date":"2024-05-20T18:50:47","date_gmt":"2024-05-20T10:50:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/303060.html"},"modified":"2024-05-20T18:51:02","modified_gmt":"2024-05-20T10:51:02","slug":"attribute%e5%92%8cproperty%e5%9c%a8%e8%8b%b1%e8%af%ad%e9%87%8c%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/303060.html","title":{"rendered":"attribute\u548cproperty\u5728\u82f1\u8bed\u91cc\u6709\u4ec0\u4e48\u533a\u522b"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26215307\/e5d6fa8e-09b2-40a4-ab9c-6251fe490c5f.webp\" alt=\"attribute\u548cproperty\u5728\u82f1\u8bed\u91cc\u6709\u4ec0\u4e48\u533a\u522b\" \/><\/p>\n<p><p>Attribute\u548cProperty\u5728\u82f1\u8bed\u91cc\u7684\u4e3b\u8981\u533a\u522b\u4f53\u73b0\u5728\u4e24\u4e2a\u65b9\u9762\uff1a\u5b83\u4eec\u5728HTML\u548cJavaScript\u4e2d\u7684\u5e94\u7528\u3001\u4ee5\u53ca\u5b83\u4eec\u7684\u66f4\u65b0\u673a\u5236\u3002\u9996\u5148\uff0cattribute\u901a\u5e38\u6307\u7684\u662fHTML\u6807\u7b7e\u4e0a\u7684\u8bbe\u7f6e\uff0c\u6bd4\u5982<code>id<\/code>\u3001<code>class<\/code>\u3001<code>style<\/code>\u7b49\uff0c\u8fd9\u4e9b\u8bbe\u7f6e\u5728\u9875\u9762\u52a0\u8f7d\u65f6\u7531HTML\u4ee3\u7801\u5b9a\u4e49\u3002Property\u5219\u662f\u6307\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u7684\u5c5e\u6027\uff0c\u8fd9\u4e9b\u5c5e\u6027\u53ef\u4ee5\u662fJavaScript\u901a\u8fc7DOM\u63a5\u53e3\u52a8\u6001\u4fee\u6539\u7684\u3002\u5176\u6b21\uff0cattribute\u7684\u503c\u5728\u521d\u59cb\u5316\u540e\u4e0d\u4f1a\u968f\u7740property\u7684\u6539\u53d8\u800c\u6539\u53d8\uff0c\u4f46property\u7684\u503c\u5374\u53ef\u4ee5\u5b9e\u65f6\u53cd\u6620\u51fa\u5bf9\u5e94attribute\u7684\u66f4\u65b0\u3002<\/p>\n<\/p>\n<p><p>\u6df1\u5165\u63a2\u7d22\u8fd9\u5176\u4e2d\u7684\u4e00\u70b9\uff1aAttribute\u4e0eProperty\u4e4b\u95f4\u7684\u66f4\u65b0\u673a\u5236\u5dee\u5f02\u3002\u5c5e\u6027\uff08Property\uff09\u662fDOM\u4e2d\u7684\u5bf9\u8c61\uff0c\u5b83\u4eec\u7684\u503c\u53ef\u4ee5\u901a\u8fc7JavaScript\u52a8\u6001\u6539\u53d8\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u901a\u8fc7JavaScript\u6539\u53d8\u4e86\u4e00\u4e2ainput\u5143\u7d20\u7684<code>value<\/code>\u5c5e\u6027\uff0c\u8fd9\u5c06\u76f4\u63a5\u5f71\u54cd\u5230\u5143\u7d20\u7684\u5f53\u524d\u503c\u3002\u76f8\u53cd\uff0c\u5c5e\u6027\uff08Attribute\uff09\u7684\u503c\u4e00\u65e6\u5728\u9875\u9762\u52a0\u8f7d\u65f6\u8bbe\u7f6e\uff0c\u5c31\u4e0d\u4f1a\u56e0\u4e3aproperty\u7684\u53d8\u5316\u800c\u81ea\u52a8\u6539\u53d8\u3002\u6bd4\u5982\uff0c\u5373\u4f7f\u6211\u4eec\u901a\u8fc7JavaScript\u6539\u53d8\u4e86\u4e0a\u8ff0input\u7684<code>value<\/code>\u5c5e\u6027\uff0c\u5176HTML\u6807\u7b7e\u4e0a\u7684<code>value<\/code>\u5c5e\u6027\u503c\u5728\u521d\u59cb\u8bbe\u7f6e\u540e\u4ecd\u7136\u4fdd\u6301\u4e0d\u53d8\uff0c\u9664\u975e\u6211\u4eec\u901a\u8fc7setAttribute\u663e\u5f0f\u5730\u4fee\u6539\u5b83\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001HTML ATTRIBUTE VS. JAVASCRIPT PROPERTY<\/h3>\n<\/p>\n<p><p>\u5728\u7406\u89e3attribute\u548cproperty\u7684\u533a\u522b\u65f6\uff0c\u9996\u5148\u8981\u6e05\u695aHTML\u548cJavaScript\u4e24\u8005\u5728\u5904\u7406\u7f51\u9875\u5143\u7d20\u65f6\u7684\u4e0d\u540c\u89d2\u8272\u3002HTML\u4f5c\u4e3a\u6807\u8bb0\u8bed\u8a00\uff0c\u4e3b\u8981\u8d1f\u8d23\u9875\u9762\u5185\u5bb9\u7684\u7ed3\u6784\u548c\u521d\u59cb\u5916\u89c2\u3002\u800cJavaScript\u5219\u4e3a\u7f51\u9875\u63d0\u4f9b\u4e86\u52a8\u6001\u4ea4\u4e92\u7684\u80fd\u529b\u3002<\/p>\n<\/p>\n<ul>\n<li>HTML attribute\u662f\u5b9a\u4e49\u5728HTML\u6807\u7b7e\u4e0a\u7684\uff0c\u5b83\u4eec\u4ee3\u8868\u4e86\u5143\u7d20\u521d\u59cb\u65f6\u7684\u8bbe\u7f6e\u3002\u4f8b\u5982\uff0c<code>&lt;input type=&quot;text&quot; value=&quot;Hello&quot;&gt;<\/code>\u4e2d\u7684<code>type<\/code>\u548c<code>value<\/code>\u5c31\u662finput\u5143\u7d20\u7684attributes\u3002\u8fd9\u4e9b\u503c\u5b9a\u4e49\u4e86\u5143\u7d20\u6700\u521d\u7684\u5916\u89c2\u548c\u884c\u4e3a\u3002<\/li>\n<li>JavaScript property\u662f\u5f53\u7f51\u9875\u88ab\u52a0\u8f7d\u5230\u6d4f\u89c8\u5668\u540e\uff0c\u7531\u6d4f\u89c8\u5668\u521b\u5efa\u7684DOM\uff08\u6587\u6863\u5bf9\u8c61\u6a21\u578b\uff09\u5bf9\u8c61\u4e2d\u7684\u5c5e\u6027\u3002\u4f8b\u5982\uff0c\u5f53\u4e0a\u8ff0\u7684HTML\u4ee3\u7801\u88ab\u52a0\u8f7d\u540e\uff0c\u6d4f\u89c8\u5668\u4f1a\u521b\u5efa\u4e00\u4e2a\u8868\u793ainput\u5143\u7d20\u7684JavaScript\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u5177\u6709<code>type<\/code>\u548c<code>value<\/code>\u7b49properties\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001ATTRIBUTE\u548cPROPERTY\u7684\u66f4\u65b0\u673a\u5236<\/h3>\n<\/p>\n<p><p>Attributes\u548cproperties\u7684\u66f4\u65b0\u673a\u5236\u4e0d\u540c\u662f\u5b83\u4eec\u4e4b\u95f4\u4e00\u4e2a\u91cd\u8981\u7684\u533a\u522b\u3002<\/p>\n<\/p>\n<ul>\n<li>\u5f53\u6211\u4eec\u4f7f\u7528JavaScript\u6539\u53d8\u6216\u8bbf\u95ee\u67d0\u4e2a\u5143\u7d20\u7684property\u65f6\uff0c\u5982\u4fee\u6539input\u7684value\uff0c\u8fd9\u4e0d\u4f1a\u5f71\u54cd\u5230HTML\u6807\u7b7e\u4e0a\u7684attribute\u3002\u8fd9\u662f\u56e0\u4e3aproperty\u7684\u6539\u53d8\u76f4\u63a5\u53d1\u751f\u5728\u5185\u5b58\u4e2d\u7684\u5bf9\u8c61\u4e0a\uff0c\u800c\u4e0d\u662f\u5728\u6807\u8bb0\u4e0a\u3002<\/li>\n<li>\u4f46\u662f\uff0c\u5f53\u6211\u4eec\u663e\u5f0f\u5730\u901a\u8fc7setAttribute\u65b9\u6cd5\u4fee\u6539\u6216\u4f7f\u7528getAttribute\u65b9\u6cd5\u8bbf\u95ee\u67d0\u4e2a\u5143\u7d20\u7684attribute\u65f6\uff0c\u8fd9\u5c31\u53ef\u4ee5\u53cd\u6620\u5728HTML\u6807\u7b7e\u4e0a\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u52a8\u6001\u5730\u4fee\u6539\u5143\u7d20\u7684\u521d\u59cb\u8bbe\u7f6e\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u9009\u62e9<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u4e86\u89e3attribute\u548cproperty\u7684\u533a\u522b\u5bf9\u4e8e\u6709\u6548\u5730\u64cd\u4f5cDOM\u548c\u907f\u514d\u4e00\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<\/p>\n<ul>\n<li>\u5f53\u9700\u8981\u83b7\u53d6\u6216\u8bbe\u7f6e\u5143\u7d20\u7684\u521d\u59cb\u503c\u3001\u6216\u9700\u8981\u4fee\u6539\u90a3\u4e9b\u4e0d\u5e38\u901a\u8fc7property\u53cd\u6620\u7684\u8bbe\u7f6e\uff08\u5982\uff0c<code>custom-attribute<\/code>\uff09\u65f6\uff0c\u5e94\u8be5\u4f7f\u7528attributes\u3002<\/li>\n<li>\u800c\u5728\u5904\u7406\u5143\u7d20\u7684\u5f53\u524d\u72b6\u6001\u6216\u5176\u52a8\u6001\u53d8\u5316\u7684\u5c5e\u6027\uff08\u6bd4\u5982\uff0c\u8f93\u5165\u5b57\u6bb5\u7684\u5185\u5bb9\uff09\u65f6\uff0c\u4f7f\u7528properties\u901a\u5e38\u662f\u66f4\u76f4\u63a5\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u6ce8\u610f\u4e8b\u9879\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u867d\u7136attribute\u548cproperty\u4e4b\u95f4\u5b58\u5728\u533a\u522b\uff0c\u4f46\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u5b83\u4eec\u53ef\u80fd\u534f\u540c\u5de5\u4f5c\u6216\u4e92\u76f8\u5f71\u54cd\u3002\u4f8b\u5982\uff0c\u67d0\u4e9bHTML attributes\u548cproperties\u662f\u540c\u6b65\u7684\uff0c\u4fee\u6539\u4e00\u4e2a\u503c\u53ef\u80fd\u4f1a\u5f71\u54cd\u5230\u53e6\u4e00\u4e2a\uff0c\u4f46\u8fd9\u5e76\u975e\u666e\u904d\u89c4\u5219\u3002<\/p>\n<\/p>\n<ul>\n<li>\u5f00\u53d1\u8005\u5e94\u5f53\u610f\u8bc6\u5230\u8fd9\u4e9b\u5dee\u5f02\uff0c\u5e76\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u64cd\u4f5cDOM\u3002<\/li>\n<li>\u5728\u4f7f\u7528JavaScript\u6846\u67b6\u6216\u5e93\u65f6\uff0c\u4e86\u89e3\u5e95\u5c42\u5982\u4f55\u5904\u7406\u8fd9\u4e9b\u533a\u522b\u4e5f\u5f88\u91cd\u8981\uff0c\u56e0\u4e3a\u4e0d\u540c\u7684\u6846\u67b6\u53ef\u80fd\u4f1a\u91c7\u7528\u4e0d\u540c\u7684\u7b56\u7565\u6765\u540c\u6b65attribute\u548cproperty\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u6df1\u5165\u7406\u89e3attribute\u548cproperty\u7684\u8fd9\u4e9b\u5173\u952e\u533a\u522b\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u548c\u51c6\u786e\u5730\u5f00\u53d1\u52a8\u6001\u4ea4\u4e92\u7684\u7f51\u9875\uff0c\u540c\u65f6\u4e5f\u907f\u514d\u4e86\u4e00\u4e9b\u7531\u6df7\u6dc6\u8fd9\u4e24\u4e2a\u6982\u5ff5\u5f15\u8d77\u7684\u5e38\u89c1\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. What is the difference between attribute and property in English?<\/strong><\/p>\n<p>Attributes and properties are two terms commonly used in the field of computer programming and web development. Though they are often used interchangeably, there is a subtle difference between the two.<\/p>\n<p>An attribute refers to a characteristic or a piece of information associated with an object or an element. In terms of web development, attributes are usually used to provide additional information about HTML elements. For example, the &quot;src&quot; attribute in an HTML image tag specifies the source URL of the image.<\/p>\n<p>On the other hand, a property refers to the value or state of an object or an element. In web development, properties can be accessed and modified using JavaScript. For example, the &quot;innerHTML&quot; property allows you to retrieve or update the content of an HTML element.<\/p>\n<p>In essence, attributes define the initial state or characteristics of an element, while properties determine the current state or value of an element.<\/p>\n","protected":false},"excerpt":{"rendered":"Attribute\u548cProperty\u5728\u82f1\u8bed\u91cc\u7684\u4e3b\u8981\u533a\u522b\u4f53\u73b0\u5728\u4e24\u4e2a\u65b9\u9762\uff1a\u5b83\u4eec\u5728HTML\u548cJavaScript\u4e2d\u7684 [&hellip;]","protected":false},"author":3,"featured_media":303085,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/303060"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=303060"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/303060\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/303085"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=303060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=303060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=303060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}