我在无序列表中有一个导航。悬停时这些图像应该会关闭

发布于 2024-09-29 16:48:13 字数 1094 浏览 0 评论 0原文

整个页面是一个 PHP 包含在主索引文件中的内容。我应该将 javascript 放在主页上吗?或者外部 javascript 文件?我引用的 li 错误吗?

<ul>
<li class="about"><a href="index.php?about"><h1>about</h1></a></li>
<li class="team"><a href="index.php?team"><h1>team</h1></a></li>
<li class="training"><a href="index.php?training"><h1>training </h1></a></li>
<li class="courses"><a href="index.php?courses"><h1>courses</h1></a></li>
<li class="register"><a href="index.php?contact"><h1>contact</h1></a></li>
</ul>

<script type="text/javascript">
$("div").hover(
function () {
$(this).addClass("navhover");
},
function () {
$(this).removeClass("navhover");
}
); 
</script>

(外部 CSS 页面)

<script type="text/css">
#center .navhover {
   background:  url(images/active.jpg) no-repeat center center; 
}

#center .about {
 background: url(../images/about.jpg) no-repeat center center;
}

The whole page is a PHP include inside of a main index file. Should I be placing the javascript on the main page? or an external javascript file? Am I referencing the li wrong?

<ul>
<li class="about"><a href="index.php?about"><h1>about</h1></a></li>
<li class="team"><a href="index.php?team"><h1>team</h1></a></li>
<li class="training"><a href="index.php?training"><h1>training </h1></a></li>
<li class="courses"><a href="index.php?courses"><h1>courses</h1></a></li>
<li class="register"><a href="index.php?contact"><h1>contact</h1></a></li>
</ul>

<script type="text/javascript">
$("div").hover(
function () {
$(this).addClass("navhover");
},
function () {
$(this).removeClass("navhover");
}
); 
</script>

(external CSS page)

<script type="text/css">
#center .navhover {
   background:  url(images/active.jpg) no-repeat center center; 
}

#center .about {
 background: url(../images/about.jpg) no-repeat center center;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此岸叶落 2024-10-06 16:48:13

为什么不直接跳过 javascript 并使用 呢


li.about {
    background: url(../images/about.jpg) no-repeat center center;
}

li.about:hover {
    background:  url(images/active.jpg) no-repeat center center;
}

Why not just skip the javascript and use


li.about {
    background: url(../images/about.jpg) no-repeat center center;
}

li.about:hover {
    background:  url(images/active.jpg) no-repeat center center;
}

?

说谎友 2024-10-06 16:48:13
<script type="text/javascript" src="script.js"></script>
<script>
$(function(){
    $("ul li").hover(function () {
          $(this).addClass("navhover");
        },function () {
          $(this).removeClass("navhover");
        }
    ); 
});

</script>
<ul>
<li class="about"><a href="index.php?about"><h1>about</h1></a></li>
<li class="team"><a href="index.php?team"><h1>team</h1></a></li>
<li class="training"><a href="index.php?training"><h1>training </h1></a></li>
<li class="courses"><a href="index.php?courses"><h1>courses</h1></a></li>
<li class="register"><a href="index.php?contact"><h1>contact</h1></a></li>
</ul>

我自己刚刚尝试过,效果非常好

<script type="text/javascript" src="script.js"></script>
<script>
$(function(){
    $("ul li").hover(function () {
          $(this).addClass("navhover");
        },function () {
          $(this).removeClass("navhover");
        }
    ); 
});

</script>
<ul>
<li class="about"><a href="index.php?about"><h1>about</h1></a></li>
<li class="team"><a href="index.php?team"><h1>team</h1></a></li>
<li class="training"><a href="index.php?training"><h1>training </h1></a></li>
<li class="courses"><a href="index.php?courses"><h1>courses</h1></a></li>
<li class="register"><a href="index.php?contact"><h1>contact</h1></a></li>
</ul>

i just tried this myself and it worked perfectly

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文