重写后的标题为:改变列表标题以响应光标移动到列表元素
P粉131455722
P粉131455722 2023-09-04 23:54:54
[CSS3讨论组]
<p>我正在创建一个链接表格,我希望当我将鼠标移动到列表链接之一时,通过更改表格标题的背景颜色使表格更加漂亮。然而,我不知道如何通过影响其较小元素来更改容器元素的属性。这是我的代码:</p> <pre class="brush:php;toolbar:false;">&lt;html lang=&quot;vi&quot;&gt; &lt;head&gt; &lt;style&gt; .toc-container { max-width: 600px; font-family: &quot;Roboto&quot;, sans-serif; background: #deff9d; border-radius: 8px; box-shadow: 0 4px 11px rgba(0, 0, 0, 0.6); } .toc-container h2.index-heading { text-transform: uppercase; font-weight: normal; margin: 0 16px; padding-top: 16px; } .table-of-contents { list-style: none; padding: 0; } .table-of-contents li.author li.blog { background: #222; transition: 400ms; list-style: none; } .table-of-contents li.author{ background-color: green; } .table-of-contents li.author li:nth-of-type(even).blog { background: #2e2e2e; } .table-of-contents li.author li:hover.blog { background: #000; } .table-of-contents li a { text-decoration: none; color: #fff; margin-left: 24px; padding: 16px 0; display: block; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div class=&quot;toc-container&quot;&gt; &lt;h2 class=&quot;index-heading&quot;&gt;heading&lt;/h2&gt; &lt;ul class=&quot;table-of-contents&quot;&gt; &lt;li class=&quot;author&quot;&gt; &lt;a href=&quot;#&quot;&gt;作者的名字&lt;/a&gt; &lt;ul&gt; &lt;li class=&quot;blog&quot;&gt; &lt;a href=&quot;#&quot;&gt;Nháp 1&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt;</pre>
P粉131455722
P粉131455722

全部回复(1)
P粉343408929

我认为使用JavaScript更容易实现这一点,你可以使用元素事件 mouseentermouseleave 来实现样式的改变,也许这可以帮到你。下面是代码:

  <script>
    const headerDiv = document.querySelector('.index-heading');
    const blogDiv = document.querySelector('.blog');
    blogDiv.addEventListener('mouseenter', function(e) {
      headerDiv.style.background = 'purple'
    })
    blogDiv.addEventListener('mouseleave', function(e) {
      headerDiv.style.background = '#deff9d'
    })
  </script>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号