{"id":25462,"date":"2024-11-24T15:00:36","date_gmt":"2024-11-24T07:00:36","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25462\/"},"modified":"2024-11-24T15:00:36","modified_gmt":"2024-11-24T07:00:36","slug":"%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad-span-%e6%a0%87%e7%ad%be%e6%98%af%e5%90%a6%e4%b8%8d%e5%9c%a8%e7%ac%ac%e4%b8%80%e8%a1%8c%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25462\/","title":{"rendered":"\u5982\u4f55\u5224\u65ad span \u6807\u7b7e\u662f\u5426\u4e0d\u5728\u7b2c\u4e00\u884c\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173061648477125.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5224\u65ad span \u6807\u7b7e\u662f\u5426\u4e0d\u5728\u7b2c\u4e00\u884c\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5224\u65ad span \u6807\u7b7e\u662f\u5426\u4e0d\u5728\u7b2c\u4e00\u884c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5224\u65ad span \u6807\u7b7e\u5782\u76f4\u4f4d\u7f6e<\/strong><\/p>\n<p>\u5728  \u6216 javascript \u4e2d\uff0c\u5224\u65ad\u4e00\u5806 span \u6807\u7b7e\u662f\u5426\u5782\u76f4\u65b9\u5411\u4e0a\u4e0d\u5728\u7b2c\u4e00\u884c\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p><strong>css<\/strong><\/p>\n<p>\u4f7f\u7528 margin-bottom \u5c5e\u6027\u3002\u5728 span \u6807\u7b7e\u4e2d\u6dfb\u52a0 margin-bottom \u5c5e\u6027\uff0c\u5f53 span \u6807\u7b7e\u6362\u884c\u5230\u4e0b\u4e00\u884c\u65f6\uff0c\u8be5\u5c5e\u6027\u4f1a\u81ea\u52a8\u751f\u6548\u3002<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>span {\n  margin-bottom: 10px;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>javascript<\/strong><\/p>\n<p>\u4f7f\u7528 [offsettop](https:\/\/developer.mozilla.org\/zh-cn\/docs\/web\/api\/element\/offsettop) \u5c5e\u6027\u3002offsettop \u5c5e\u6027\u8868\u793a\u5143\u7d20\u8ddd\u79bb\u5176\u7236\u5143\u7d20\u9876\u90e8\u8fb9\u754c\u7684\u4f4d\u7f6e\u3002\u5982\u679c span \u6807\u7b7e\u4e0d\u5728\u7b2c\u4e00\u884c\uff0c\u5219\u5b83\u7684 offsettop \u503c\u5c06\u5927\u4e8e 0\u3002<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>const spans = document.querySelectorAll('span');\n\nfor (const span of spans) {\n  if (span.offsetTop &gt; 0) {\n    span.style.marginTop = '10px';\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u9700\u8981\u6ce8\u610f\uff0c\u5728\u4f7f\u7528 javascript \u65b9\u6cd5\u65f6\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u8bbe\u7f6e margin-top \u5c5e\u6027\uff0c\u800c\u4e0d\u662f margin-bottom \u5c5e\u6027\u3002<\/p>\n<p><strong>\u5176\u4ed6\u65b9\u6cd5<\/strong><\/p>\n<p>\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u6765\u89e3\u51b3\u5782\u76f4\u95f4\u8ddd\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 flex \u5e03\u5c40\uff0c\u5e76\u8bbe\u7f6e gap \u5c5e\u6027\u3002<\/li>\n<li>\u4f7f\u7528 css grid \u5e03\u5c40\u3002<\/li>\n<li>\u624b\u52a8\u8ba1\u7b97\u95f4\u8ddd\uff0c\u5e76\u5728\u5143\u7d20\u6362\u884c\u540e\u52a8\u6001\u8c03\u6574\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5224\u65ad span \u6807\u7b7e\u662f\u5426\u4e0d\u5728\u7b2c\u4e00\u884c\uff1f\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>\u5224\u65ad span \u6807\u7b7e\u5782\u76f4\u4f4d\u7f6e \u5728 \u6216 javascript \u4e2d\uff0c\u5224\u65ad\u4e00\u5806 span \u6807\u7b7e\u662f\u5426\u5782\u76f4\u65b9\u5411\u4e0a\u4e0d\u5728\u7b2c\u4e00\u884c\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a css \u4f7f\u7528 margin-bottom \u5c5e\u6027\u3002\u5728 span \u6807\u7b7e\u4e2d\u6dfb\u52a0 margin-bottom \u5c5e\u6027\uff0c\u5f53 span \u6807\u7b7e\u6362\u884c\u5230\u4e0b\u4e00\u884c\u65f6\uff0c\u8be5\u5c5e\u6027\u4f1a\u81ea\u52a8\u751f\u6548\u3002 \u793a\u4f8b\u4ee3\u7801\uff1a span { margin-bottom: 10px; } \u767b\u5f55\u540e\u590d\u5236 javascript \u4f7f\u7528 [offsettop](https:\/\/developer.mozilla.org\/zh-cn\/docs\/web\/api\/element\/offsettop) \u5c5e\u6027\u3002offsettop \u5c5e\u6027\u8868\u793a\u5143\u7d20\u8ddd\u79bb\u5176\u7236\u5143\u7d20\u9876\u90e8\u8fb9\u754c\u7684\u4f4d\u7f6e\u3002\u5982\u679c span \u6807\u7b7e\u4e0d\u5728\u7b2c\u4e00\u884c\uff0c\u5219\u5b83\u7684 offsettop \u503c\u5c06\u5927\u4e8e 0\u3002 \u793a\u4f8b\u4ee3\u7801\uff1a const spans = document.querySelectorAll(&#8216;span&#8217;); for (const span of spans) { if (span.offsetTop &gt; 0) { span.style.marginTop = &#8217;10px&#8217;; [&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-25462","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25462","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=25462"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25462\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}