{"id":3458,"date":"2024-11-10T12:26:19","date_gmt":"2024-11-10T04:26:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3458\/"},"modified":"2024-11-10T12:26:19","modified_gmt":"2024-11-10T04:26:19","slug":"js%e5%a6%82%e4%bd%95%e6%90%9c%e7%b4%a2%e8%a1%a8%e6%a0%bc","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3458\/","title":{"rendered":"js\u5982\u4f55\u641c\u7d22\u8868\u683c"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\u641c\u7d22\u8868\u683c\u6570\u636e\u7684\u65b9\u6cd5\u4e3a\uff1a\u83b7\u53d6\u8868\u683c\u5f15\u7528\uff1b\u83b7\u53d6\u8868\u5934\uff1b\u83b7\u53d6\u8868\u4f53\uff1b\u5faa\u73af\u904d\u5386\u8868\u4f53\u884c\u68c0\u67e5\u662f\u5426\u5339\u914d\u641c\u7d22\u8bcd\uff1b\u5bf9\u627e\u5230\u7684\u5339\u914d\u884c\u8fdb\u884c\u5904\u7406\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110812452618497.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u641c\u7d22\u8868\u683c\u63d2\u56fe\" alt=\"js\u5982\u4f55\u641c\u7d22\u8868\u683c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5728 JavaScript \u4e2d\u641c\u7d22\u8868\u683c<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\u641c\u7d22\u8868\u683c\u6570\u636e\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a<\/p>\n<p><strong>1. \u83b7\u53d6\u8868\u683c\u5f15\u7528<\/strong><\/p>\n<p>\u4f7f\u7528 document.querySelector \u6216 document.getElementById \u65b9\u6cd5\u83b7\u53d6 DOM \u4e2d\u8868\u683c\u7684\u5f15\u7528\u3002<\/p>\n<pre>const table = document.querySelector(\"table\");<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. \u83b7\u53d6\u8868\u5934<\/strong><\/p>\n<p>\u4f7f\u7528 table.tHead \u5c5e\u6027\u83b7\u53d6\u8868\u683c\u7684\u8868\u5934\u3002\u8868\u5934\u5305\u542b\u5217\u6807\u9898\u3002<\/p>\n<pre>const tableHead = table.tHead;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u83b7\u53d6\u8868\u4f53<\/strong><\/p>\n<p>\u4f7f\u7528 table.tBodies \u5c5e\u6027\u83b7\u53d6\u8868\u683c\u7684\u8868\u4f53\u3002\u8868\u4f53\u5305\u542b\u6570\u636e\u884c\u3002<\/p>\n<pre>const tableBody = table.tBodies[0]; \/\/ \u901a\u5e38\u83b7\u53d6\u7b2c\u4e00\u4e2a\u8868\u4f53<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. \u641c\u7d22\u6570\u636e<\/strong><\/p>\n<p>\u5faa\u73af\u904d\u5386\u8868\u4f53\u884c\u5e76\u68c0\u67e5\u6bcf\u884c\u7684\u5355\u5143\u683c\u662f\u5426\u4e0e\u641c\u7d22\u8bcd\u5339\u914d\u3002<\/p>\n<pre>const searchTerm = \"John\";\nlet foundRowIndex = -1;\n\nfor (let i = 0; i &lt; tableBody.rows.length; i++) {\n  const row = tableBody.rows[i];\n  for (let j = 0; j &lt; row.cells.length; j++) {\n    const cell = row.cells[j];\n    if (cell.textContent.toLowerCase().includes(searchTerm.toLowerCase())) {\n      foundRowIndex = i;\n      break;\n    }\n  }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>5. \u5904\u7406\u7ed3\u679c<\/strong><\/p>\n<p>\u5982\u679c\u627e\u5230\u5339\u914d\u7684\u884c\uff0c\u5c06\u5176\u9ad8\u4eae\u663e\u793a\u6216\u4ee5\u5176\u4ed6\u65b9\u5f0f\u6807\u8bb0\u3002<\/p>\n<pre>if (foundRowIndex !== -1) {\n  const foundRow = tableBody.rows[foundRowIndex];\n  foundRow.style.backgroundColor = \"yellow\";\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u641c\u7d22\u8868\u683c\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>\u5728 javascript \u4e2d\u641c\u7d22\u8868\u683c\u6570\u636e\u7684\u65b9\u6cd5\u4e3a\uff1a\u83b7\u53d6\u8868\u683c\u5f15\u7528\uff1b\u83b7\u53d6\u8868\u5934\uff1b\u83b7\u53d6\u8868\u4f53\uff1b\u5faa\u73af\u904d\u5386\u8868\u4f53\u884c\u68c0\u67e5\u662f\u5426\u5339\u914d\u641c\u7d22\u8bcd\uff1b\u5bf9\u627e\u5230\u7684\u5339\u914d\u884c\u8fdb\u884c\u5904\u7406\u3002 \u5728 JavaScript \u4e2d\u641c\u7d22\u8868\u683c \u5728 JavaScript \u4e2d\u641c\u7d22\u8868\u683c\u6570\u636e\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a 1. \u83b7\u53d6\u8868\u683c\u5f15\u7528 \u4f7f\u7528 document.querySelector \u6216 document.getElementById \u65b9\u6cd5\u83b7\u53d6 DOM \u4e2d\u8868\u683c\u7684\u5f15\u7528\u3002 const table = document.querySelector(&#8220;table&#8221;); \u767b\u5f55\u540e\u590d\u5236 2. \u83b7\u53d6\u8868\u5934 \u4f7f\u7528 table.tHead \u5c5e\u6027\u83b7\u53d6\u8868\u683c\u7684\u8868\u5934\u3002\u8868\u5934\u5305\u542b\u5217\u6807\u9898\u3002 const tableHead = table.tHead; \u767b\u5f55\u540e\u590d\u5236 3. \u83b7\u53d6\u8868\u4f53 \u4f7f\u7528 table.tBodies \u5c5e\u6027\u83b7\u53d6\u8868\u683c\u7684\u8868\u4f53\u3002\u8868\u4f53\u5305\u542b\u6570\u636e\u884c\u3002 const tableBody = table.tBodies[0]; \/\/ \u901a\u5e38\u83b7\u53d6\u7b2c\u4e00\u4e2a\u8868\u4f53 \u767b\u5f55\u540e\u590d\u5236 4. \u641c\u7d22\u6570\u636e \u5faa\u73af\u904d\u5386\u8868\u4f53\u884c\u5e76\u68c0\u67e5\u6bcf\u884c\u7684\u5355\u5143\u683c\u662f\u5426\u4e0e\u641c\u7d22\u8bcd\u5339\u914d\u3002 const searchTerm = &#8220;John&#8221;; let foundRowIndex = [&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-3458","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3458","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=3458"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3458\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}