js里面怎么使用两个按钮切换四个div的内容?

两个按钮,上一页下一页,我有四个DIV,点击一次显示一个内容其他隐藏,

<form>


<div>1</div>

<div>2</div>

<div>3</div>

<div>4</div>

</form>

<button>上一页</button>

<button>下一页</button>


一只萌萌小番薯
浏览 1312回答 3
3回答

茅侃侃

你给没给div添加上id,比如div1,div2,div3,div4,并定义一个隐藏的css类名和相应配置,比如hiden你用一个标志变量来记录当前该展示那一页,比如index,初始为1然后你每次切换(按按钮)就处理index值,然后就可以查询到该显示的div的ID,把其中class属性去除上hiden,把上次没有hiden类的添加上即可。

摇曳的蔷薇

看题主的代码,我理解为:题主想做一个通过点击按钮轮番切换 div 的功能。以下为实现代码,纯 js DOM 操作,没有使用 JQuery<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <meta http-equiv="X-UA-Compatible" content="ie=edge">&nbsp; &nbsp; <title>Document</title>&nbsp; &nbsp; <style>&nbsp; &nbsp; &nbsp; &nbsp; div {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height: 100px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width: 100px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float: left;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display: none;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; div:nth-child(1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background-color:chocolate;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; div:nth-child(2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background-color:darkgoldenrod;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; div:nth-child(3) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background-color:darkmagenta;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; .active {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display: block;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </style></head><body>&nbsp; &nbsp; <form id="box">&nbsp; &nbsp; &nbsp; &nbsp; <div class="active">1</div>&nbsp; &nbsp; &nbsp; &nbsp; <div>2</div>&nbsp; &nbsp; &nbsp; &nbsp; <div>3</div>&nbsp; &nbsp; </form>&nbsp; &nbsp; <button onclick="pre()">上一页</button>&nbsp; &nbsp; <button onclick="next()">下一页</button>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; function next() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let divEl = document.getElementsByClassName('active')[0],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nextEl = divEl.nextElementSibling;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; divEl.className = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (nextEl) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nextEl.className = 'active';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('box').firstElementChild.className = 'active';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; function pre() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let divEl = document.getElementsByClassName('active')[0],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; preEl = divEl.previousElementSibling;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; divEl.className = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (preEl) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; preEl.className = 'active';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('box').lastElementChild.className = 'active';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript