{"id":2174,"date":"2024-11-10T12:59:30","date_gmt":"2024-11-10T04:59:30","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2174\/"},"modified":"2024-11-10T12:59:30","modified_gmt":"2024-11-10T04:59:30","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-javascript-%e6%88%96-jquery-%e8%8e%b7%e5%8f%96-dom-%e8%8a%82%e7%82%b9%e4%b8%ad%e7%9a%84%e7%89%b9%e5%ae%9a%e6%96%87%e6%9c%ac%e5%86%85%e5%ae%b9%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2174\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 JavaScript \u6216 jQuery \u83b7\u53d6 DOM \u8282\u70b9\u4e2d\u7684\u7279\u5b9a\u6587\u672c\u5185\u5bb9\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173068218326048.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 JavaScript \u6216 jQuery \u83b7\u53d6 DOM \u8282\u70b9\u4e2d\u7684\u7279\u5b9a\u6587\u672c\u5185\u5bb9\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 JavaScript \u6216 jQuery \u83b7\u53d6 DOM \u8282\u70b9\u4e2d\u7684\u7279\u5b9a\u6587\u672c\u5185\u5bb9\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5229\u7528 javascript \/ jquery \u83b7\u53d6 dom \u8282\u70b9\u4e2d\u7684\u7279\u5b9a\u6587\u672c\u5185\u5bb9<\/strong><\/p>\n<p>\u5728\u7ed9\u5b9a\u7684 html \u7ed3\u6784\u4e2d\uff0c<\/p>\n<pre>&lt;li onclick=\"ss(this)\"&gt;&lt;div&gt;\u4fe1\u606f\u697c&lt;\/div&gt;&lt;img  src=\"...\" id=\"tree1\" \/ alt=\"\u5982\u4f55\u4f7f\u7528 JavaScript \u6216 jQuery \u83b7\u53d6 DOM \u8282\u70b9\u4e2d\u7684\u7279\u5b9a\u6587\u672c\u5185\u5bb9\uff1f\" &gt;&lt;\/li&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u76ee\u6807\uff1a<\/strong>\u83b7\u53d6 \u4fe1\u606f\u697c \u8fd9\u51e0\u4e2a\u5b57\u3002<\/p>\n<p><strong>javascript\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>function ss(el) {\n  console.log(el.parentnode.parentnode.textcontent);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>jquery\uff1a<\/strong><\/p>\n<pre>$(\"li[onclick]\").click(function() {\n  console.log($(this).parent().parent().text());\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<ul>\n<li>el.parentnode.parentnode.textcontent \u6216\u8005 $(this).parent().parent().text() \u53ef\u4ee5\u904d\u5386\u5230 li \u5143\u7d20\u7684\u7236\u5143\u7d20 (div)\uff0c\u518d\u904d\u5386\u5230\u5176\u7236\u5143\u7d20 (ul)\uff0c\u5e76\u83b7\u53d6\u5b83\u7684\u6587\u672c\u5185\u5bb9\u3002\u8fd9\u4e2a\u6587\u672c\u5185\u5bb9\u5c31\u662f \u4fe1\u606f\u697c\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528 JavaScript \u6216 jQuery \u83b7\u53d6 DOM \u8282\u70b9\u4e2d\u7684\u7279\u5b9a\u6587\u672c\u5185\u5bb9\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>\u5229\u7528 javascript \/ jquery \u83b7\u53d6 dom \u8282\u70b9\u4e2d\u7684\u7279\u5b9a\u6587\u672c\u5185\u5bb9 \u5728\u7ed9\u5b9a\u7684 html \u7ed3\u6784\u4e2d\uff0c &lt;li onclick=&#8221;ss(this)&#8221;&gt;&lt;div&gt;\u4fe1\u606f\u697c&lt;\/div&gt;&lt;img src=&#8221;&#8230;&#8221; id=&#8221;tree1&#8243; \/ alt=&#8221;\u5982\u4f55\u4f7f\u7528 JavaScript \u6216 jQuery \u83b7\u53d6 DOM \u8282\u70b9\u4e2d\u7684\u7279\u5b9a\u6587\u672c\u5185\u5bb9\uff1f&#8221; &gt;&lt;\/li&gt; \u767b\u5f55\u540e\u590d\u5236 \u76ee\u6807\uff1a\u83b7\u53d6 \u4fe1\u606f\u697c \u8fd9\u51e0\u4e2a\u5b57\u3002 javascript\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b function ss(el) { console.log(el.parentnode.parentnode.textcontent); } \u767b\u5f55\u540e\u590d\u5236 jquery\uff1a $(&#8220;li[onclick]&#8221;).click(function() { console.log($(this).parent().parent().text()); }); \u767b\u5f55\u540e\u590d\u5236 \u89e3\u91ca\uff1a el.parentnode.parentnode.textcontent \u6216\u8005 $(this).parent().parent().text() \u53ef\u4ee5\u904d\u5386\u5230 li \u5143\u7d20\u7684\u7236\u5143\u7d20 (div)\uff0c\u518d\u904d\u5386\u5230\u5176\u7236\u5143\u7d20 (ul)\uff0c\u5e76\u83b7\u53d6\u5b83\u7684\u6587\u672c\u5185\u5bb9\u3002\u8fd9\u4e2a\u6587\u672c\u5185\u5bb9\u5c31\u662f \u4fe1\u606f\u697c\u3002 \u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528 JavaScript \u6216 jQuery [&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-2174","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2174","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=2174"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2174\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}