{"id":3482,"date":"2024-11-10T10:06:35","date_gmt":"2024-11-10T02:06:35","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3482\/"},"modified":"2024-11-10T10:06:35","modified_gmt":"2024-11-10T02:06:35","slug":"js%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8hover","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3482\/","title":{"rendered":"js\u5982\u4f55\u4f7f\u7528hover"},"content":{"rendered":"<blockquote><p>\n  javascript\u4e2d\u7684hover\u6548\u679c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528onmouseover\u548conmouseout\u4e8b\u4ef6\u4f7f\u7528addeventlistener()\u65b9\u6cd5\u4f7f\u7528jquery(\u7b80\u5316\u8bed\u6cd5)\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110813241766612.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u4f7f\u7528hover\u63d2\u56fe\" alt=\"js\u5982\u4f55\u4f7f\u7528hover\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 JavaScript \u5b9e\u73b0 hover \u6548\u679c<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>hover \u662f\u6307\u5f53\u9f20\u6807\u60ac\u505c\u5728\u67d0\u4e2a\u5143\u7d20\u4e0a\u65f6\uff0c\u8be5\u5143\u7d20\u7684\u6837\u5f0f\u6216\u884c\u4e3a\u53d1\u751f\u6539\u53d8\u3002\u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0 hover \u6548\u679c\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 onmouseover \u548c onmouseout \u4e8b\u4ef6<\/strong><\/p>\n<pre>element.onmouseover = function() {\n  \/\/ \u9f20\u6807\u60ac\u505c\u65f6\u6267\u884c\u7684\u4ee3\u7801\n};\n\nelement.onmouseout = function() {\n  \/\/ \u9f20\u6807\u79bb\u5f00\u65f6\u6267\u884c\u7684\u4ee3\u7801\n};<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f8b\u5982\uff1a\u8981\u8ba9 div \u5728\u60ac\u505c\u65f6\u53d8\u4e3a\u7ea2\u8272\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>const div = document.querySelector('div');\n\ndiv.onmouseover = function() {\n  div.style.backgroundColor = 'red';\n};\n\ndiv.onmouseout = function() {\n  div.style.backgroundColor = '';\n};<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. \u4f7f\u7528 addEventListener() \u65b9\u6cd5<\/strong><\/p>\n<pre>element.addEventListener('mouseover', function() {\n  \/\/ \u9f20\u6807\u60ac\u505c\u65f6\u6267\u884c\u7684\u4ee3\u7801\n});\n\nelement.addEventListener('mouseout', function() {\n  \/\/ \u9f20\u6807\u79bb\u5f00\u65f6\u6267\u884c\u7684\u4ee3\u7801\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f8b\u5982\uff1a\u8981\u8ba9 button \u5728\u60ac\u505c\u65f6\u663e\u793a\u4e00\u4e2a\u63d0\u793a\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>const button = document.querySelector('button');\n\nbutton.addEventListener('mouseover', function() {\n  button.title = '\u63d0\u793a';\n});\n\nbutton.addEventListener('mouseout', function() {\n  button.title = '';\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u4f7f\u7528 jQuery<\/strong><\/p>\n<p>jQuery \u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5316 hover \u5904\u7406\u7684\u8bed\u6cd5\uff1a<\/p>\n<pre>$(element).hover(\n  function() {\n    \/\/ \u9f20\u6807\u60ac\u505c\u65f6\u6267\u884c\u7684\u4ee3\u7801\n  },\n  function() {\n    \/\/ \u9f20\u6807\u79bb\u5f00\u65f6\u6267\u884c\u7684\u4ee3\u7801\n  }\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f8b\u5982\uff1a\u8981\u8ba9 paragraph \u5728\u60ac\u505c\u65f6\u53d8\u4e3a\u7c97\u4f53\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>$('p').hover(\n  function() {\n    $(this).css('font-weight', 'bold');\n  },\n  function() {\n    $(this).css('font-weight', 'normal');\n  }\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u4f7f\u7528 onmouseover\/onmouseout\u3001addEventListener() \u6216 jQuery\uff0c\u60a8\u53ef\u4ee5\u5728 JavaScript \u4e2d\u8f7b\u677e\u5b9e\u73b0 hover \u6548\u679c\uff0c\u4ee5\u589e\u5f3a Web \u5e94\u7528\u7a0b\u5e8f\u7684\u4ea4\u4e92\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u4f7f\u7528hover\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\u7684hover\u6548\u679c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528onmouseover\u548conmouseout\u4e8b\u4ef6\u4f7f\u7528addeventlistener()\u65b9\u6cd5\u4f7f\u7528jquery(\u7b80\u5316\u8bed\u6cd5) \u5982\u4f55\u4f7f\u7528 JavaScript \u5b9e\u73b0 hover \u6548\u679c \u7b80\u4ecb hover \u662f\u6307\u5f53\u9f20\u6807\u60ac\u505c\u5728\u67d0\u4e2a\u5143\u7d20\u4e0a\u65f6\uff0c\u8be5\u5143\u7d20\u7684\u6837\u5f0f\u6216\u884c\u4e3a\u53d1\u751f\u6539\u53d8\u3002\u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0 hover \u6548\u679c\uff1a 1. \u4f7f\u7528 onmouseover \u548c onmouseout \u4e8b\u4ef6 element.onmouseover = function() { \/\/ \u9f20\u6807\u60ac\u505c\u65f6\u6267\u884c\u7684\u4ee3\u7801 }; element.onmouseout = function() { \/\/ \u9f20\u6807\u79bb\u5f00\u65f6\u6267\u884c\u7684\u4ee3\u7801 }; \u767b\u5f55\u540e\u590d\u5236 \u4f8b\u5982\uff1a\u8981\u8ba9 div \u5728\u60ac\u505c\u65f6\u53d8\u4e3a\u7ea2\u8272\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a const div = document.querySelector(&#8216;div&#8217;); div.onmouseover = function() { div.style.backgroundColor = &#8216;red&#8217;; }; div.onmouseout = function() { div.style.backgroundColor = [&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-3482","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3482","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=3482"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3482\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}