jquery - 如何使用 JQuery 在导航栏中更改列表项/元素的背景颜色?
问题描述
我只是在学习 JQuery,为了测试我自己,我正在创建一个简单的网页,它有一个由列表元素、页眉和页脚组成的导航栏。每当我单击任何项目时,我都想更改导航栏列表元素的背景颜色。
我尝试使用$(id-name)
选择器和 .click() 方法来更改背景颜色,但它只是更改文本的背景颜色,而不是包含填充、宽度/高度和边框等的整个元素。
这是我的 HTML 代码:
<div id="nav">
<ul>
<li id="t1" ><a style="background-color: #505152;" href="#">ELEMENT 1</a></li>
<li id="t2"><a href="#">ELEMENT 2</a></li>
<li id="t3"><a href="#">ELEMENT 3</a></li>
<li id="t4"><a href="#">ELEMENT 4</a></li>
</ul>
</div>
<div id="body">
<div id="text1" class="allText">This is Text 1</div>
<div id="text2" class="allText" style="display: none;" >This is text 2</div>
<div id="text3" class="allText" style="display: none;">This is text 3</div>
<div id="text4" class="allText" style="display: none;">This is text 4</div>
</div>
这是我的 CSS 代码:
body{
font-family: Helvetica;
margin: 0;
padding: 0;
}
#body{
background-color: #fcfcfc;
width: 75%;
float: left;;
height: 450;
}
#nav{
background-color: #e1e4e6;
width: 25%;
float: left;
height: 440px;
}
ul{
list-style: none;
}
ul li a{
text-decoration: none;
padding: 20px;
color: white;
background-color: black;
margin-bottom: 100px;
font-style: italic;
}
ul li{
margin-top: 40px;
}
这是我的 jQuery 代码:
$(document).ready(function(){
$("#text1").css("background-color", "#38ceff");
console.log("enter in script");
$("#t1").click(function(){
$("li").eq(0).css("background-color", "#505152");
$("li").eq(1).css("background-color", "black");
$("li").eq(2).css("background-color", "black");
$("li").eq(3).css("background-color", "black");
$("#text1").show();
$("#text2").hide();
$("#text3").hide();
$("#text4").hide();
});
$("#t2").click(function(){
$("li").eq(0).css("background-color", "black");
$("li").eq(1).css("background-color", "#505152");
$("li").eq(2).css("background-color", "black");
$("li").eq(3).css("background-color", "black");
$("#text1").hide();
$("#text2").show();
$("#text3").hide();
$("#text4").hide();
});
$("#t3").click(function(){
$("li").eq(0).css("background-color", "black");
$("li").eq(1).css("background-color", "black");
$("li").eq(2).css("background-color", "#505152");
$("li").eq(3).css("background-color", "black");
$("#text1").hide();
$("#text2").hide();
$("#text3").show();
$("#text4").hide();
});
$("#t4").click(function(){
$("li").eq(0).css("background-color", "black");
$("li").eq(1).css("background-color", "black");
$("li").eq(2).css("background-color", "black");
$("li").eq(3).css("background-color", "#505152");
$("#text1").hide();
$("#text2").hide();
$("#text3").hide();
$("#text4").show();
});
});
任何帮助将不胜感激。
解决方案
$(function(){
$("#text1").css("background-color", "#38ceff");
console.log("enter in script");
$("#t1").click(function(){
$("li a").eq(0).css("background-color", "#505152");
$("li a").eq(1).css("background-color", "black");
$("li a").eq(2).css("background-color", "black");
$("li a").eq(3).css("background-color", "black");
$("#text1").show();
$("#text2").hide();
$("#text3").hide();
$("#text4").hide();
});
$("#t2").click(function(){
$("li a").eq(0).css("background-color", "black");
$("li a").eq(1).css("background-color", "#505152");
$("li a").eq(2).css("background-color", "black");
$("li a").eq(3).css("background-color", "black");
$("#text1").hide();
$("#text2").show();
$("#text3").hide();
$("#text4").hide();
});
$("#t3").click(function(){
$("li a").eq(0).css("background-color", "black");
$("li a").eq(1).css("background-color", "black");
$("li a").eq(2).css("background-color", "#505152");
$("li a").eq(3).css("background-color", "black");
$("#text1").hide();
$("#text2").hide();
$("#text3").show();
$("#text4").hide();
});
$("#t4").click(function(){
$("li a").eq(0).css("background-color", "black");
$("li a").eq(1).css("background-color", "black");
$("li a").eq(2).css("background-color", "black");
$("li a").eq(3).css("background-color", "#505152");
$("#text1").hide();
$("#text2").hide();
$("#text3").hide();
$("#text4").show();
});
});
body{
font-family: Helvetica;
margin: 0;
padding: 0;
}
#body{
background-color: #fcfcfc;
width: 75%;
float: left;;
height: 450;
}
#nav{
background-color: #e1e4e6;
width: 25%;
float: left;
height: 440px;
}
ul{
list-style: none;
}
ul li a{
text-decoration: none;
padding: 20px;
color: white;
background-color: black;
margin-bottom: 100px;
font-style: italic;
}
ul li{
margin-top: 40px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="nav">
<ul>
<li id="t1" ><a style="background-color: #505152;" href="#">ELEMENT 1</a></li>
<li id="t2"><a href="#">ELEMENT 2</a></li>
<li id="t3"><a href="#">ELEMENT 3</a></li>
<li id="t4"><a href="#">ELEMENT 4</a></li>
</ul>
</div>
<div id="body">
<div id="text1" class="allText">This is Text 1</div>
<div id="text2" class="allText" style="display: none;" >This is text 2</div>
<div id="text3" class="allText" style="display: none;">This is text 3</div>
<div id="text4" class="allText" style="display: none;">This is text 4</div>
</div>
这是你的 javascript
$("#text1").css("background-color", "#38ceff");
console.log("enter in script");
$("#t1").click(function(){
$("li a").eq(0).css("background-color", "#505152");
$("li a").eq(1).css("background-color", "black");
$("li a").eq(2).css("background-color", "black");
$("li a").eq(3).css("background-color", "black");
$("#text1").show();
$("#text2").hide();
$("#text3").hide();
$("#text4").hide();
});
$("#t2").click(function(){
$("li a").eq(0).css("background-color", "black");
$("li a").eq(1).css("background-color", "#505152");
$("li a").eq(2).css("background-color", "black");
$("li a").eq(3).css("background-color", "black");
$("#text1").hide();
$("#text2").show();
$("#text3").hide();
$("#text4").hide();
});
$("#t3").click(function(){
$("li a").eq(0).css("background-color", "black");
$("li a").eq(1).css("background-color", "black");
$("li a").eq(2).css("background-color", "#505152");
$("li a").eq(3).css("background-color", "black");
$("#text1").hide();
$("#text2").hide();
$("#text3").show();
$("#text4").hide();
});
$("#t4").click(function(){
$("li a").eq(0).css("background-color", "black");
$("li a").eq(1).css("background-color", "black");
$("li a").eq(2).css("background-color", "black");
$("li a").eq(3).css("background-color", "#505152");
$("#text1").hide();
$("#text2").hide();
$("#text3").hide();
$("#text4").show();
});
});
推荐阅读
- excel - 从具有 8 列和两行的工作表中:第一行是第二行中值的排名。将排名靠前的值减 1?
- javascript - 将非结构化对象数组转换为 CSV 格式 - JS
- flutter - 从另一个独立于调用树的有状态小部件触发包含 setState() 的方法
- javascript - 如何修复用 Javascript 和 HTML 编写的复利?
- ios - iOS 上的 userDomainMask 和 localDomainMask 等 FileManager.SearchPathDomainMask 有什么区别?
- mysql - 我在 SQL 中一周大。我不明白这个错误,“第 1 行的错误 1054 (42S22):‘字段列表’中的未知列‘S.hacker_id’”
- javascript - 如何从 TMDB API 中提取更多数据?
- javascript - 使用 JSDoc 记录数组数组的最佳方法是什么?
- reactjs - 用 Jest 测试一个 React 组件
- regex - Pandas 使用正则表达式有条件地更改单元格