html js css如何关联_html+css +js 选项卡

先放效果图吧

4830c6f62c03b6e40e571682ca853307.png

第一种 html+css +jquery 记得引入juqery文件 我偏向于第一种,eq是遍历

jQuery ±éÀú - eq() ·½·¨​www.w3school.com.cn

这是他的官方解释,点击链接即可

307def2874e04c7314d734a46313a481.png

上图是部分代码图 样式自己设置即可

完整demo

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.title {
width: 200px;
display: flex;
}
.title div {
height: 30px;
font-size: 16px;
flex: 1;
border: 1px solid;
text-align: center;
line-height: 30px;
}
.active {
color: red;
}
.content {
width: 200px;
border: 1px solid;
}
.content div {
width: 100%;
height: 200px;
font-size: 18px;
display: none;
}
.content div:nth-child(1) {
background-color: green;
display: block;
}
.content div:nth-child(2) {
background-color: yellow;
}
.content div:nth-child(3) {
background-color: goldenrod;
}
</style>
</head>
<body>
<div class="title">
<div class="active">语文</div>
<div>数学</div>
<div>英语</div>
</div>
<div class="content">
<div>语文</div>
<div>数学</div>
<div>英语</div>
</div>
</body>
<script src='./js/jquery.js'></script>
<script>
$(function() {
$(".title div").click(function() {
$('.title div').removeClass('active')
$(this).addClass('active')
$('.content div').hide()
$('.content div').eq($('.title div').index(this)).show()
})
})
</script>
</html>

//02 html+css+javascript效果图和juqery一样我直接贴代码图吧

a3609a072afb32a4715c4618b393ff89.png

样式可以写哦,

下面我放一下完整demo 用的是循环遍历的写法

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
li {
list-style: none;
}
.title {
width: 200px;
display: flex;
}
.title li {
height: 30px;
font-size: 16px;
flex: 1;
border: 1px solid;
text-align: center;
line-height: 30px;
}
.act {
color: red;
}
.content {
width: 200px;
border: 1px solid;
}
.content div {
width: 100%;
height: 200px;
font-size: 18px;
display: none;
}
.content div:nth-child(1) {
background-color: green;
display: block;
}
.content div:nth-child(2) {
background-color: yellow;
}
.content div:nth-child(3) {
background-color: goldenrod;
}
</style>
</head>
<body>
<nav class="title">
<li class="act">语文</li>
<li>数学</li>
<li>英语</li>
</nav>
<div class="content">
<div class="tab">语文</div>
<div class="tab">数学</div>
<div class="tab">英语</div>
</div>
</body>
<script>
window.onload = function() {
var title = document.getElementsByTagName('li')
var cont = document.getElementsByClassName('tab')
for (var i = 0; i < title.length; i++) {
title[i].index = i;
title[i].onclick = function() {
for (var i = 0; i < title.length; i++) {
title[i].className = '';
cont[i].style.display = 'none'
}
this.className = 'act';
cont[this.index].style.display = 'block'
}
for (var m = 1; m < title.length; m++) {
title[m].className = '';
cont[m].style.display = 'none'
}
}
}
</script>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的选项卡界面的HTMLCSS和JavaScript实现。请将以下代码保存为tab.html文件并在浏览器中打开查看效果。 ```html <!DOCTYPE html> <html> <head> <title>Tab</title> <style> body { background-color: #f2f2f2; font-family: Arial, sans-serif; } .tab { border: 1px solid #ccc; border-radius: 5px; overflow: hidden; } .tab button { background-color: #fff; border: none; outline: none; cursor: pointer; padding: 10px 20px; font-size: 16px; font-family: Arial, sans-serif; border-bottom: 1px solid #ccc; } .tab button.active { background-color: #ccc; } .tabcontent { display: none; padding: 20px; background-color: #fff; border: 1px solid #ccc; } </style> </head> <body> <div class="tab"> <button class="tablinks" onclick="openTab(event, 'tab1')" id="defaultOpen">Tab 1</button> <button class="tablinks" onclick="openTab(event, 'tab2')">Tab 2</button> <button class="tablinks" onclick="openTab(event, 'tab3')">Tab 3</button> </div> <div id="tab1" class="tabcontent"> <h2>Tab 1 content</h2> <p>This is the content of Tab 1.</p> </div> <div id="tab2" class="tabcontent"> <h2>Tab 2 content</h2> <p>This is the content of Tab 2.</p> </div> <div id="tab3" class="tabcontent"> <h2>Tab 3 content</h2> <p>This is the content of Tab 3.</p> </div> <script> function openTab(evt, tabName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } document.getElementById(tabName).style.display = "block"; evt.currentTarget.className += " active"; } // 默认打开第一个选项卡 document.getElementById("defaultOpen").click(); </script> </body> </html> ``` 这个选项卡界面使用了HTMLCSS和JavaScript来实现,其中HTML用于面结构,CSS用于样式设计,JavaScript用于选项卡的切换。界面包含一个选项卡导航栏和三个选项卡内容。当用户点击导航栏中的选项卡时,JavaScript将会显示对应的内容,并将被选中的选项卡按钮高亮显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值