JavaScript 函数通过发送下一个元素的整数来更改元素的可见性
javascript function to change visibility of elements by sending the integer of the next element
>我有一个包含元素的页面,例如
<div id="div1" style="display: block;">Stuff</div>
<div id="div2" style="display: none;">Stuff</div>
<div id="div3" style="display: none;">Stuff</div>
<div id="div4" style="display: none;">Stuff</div>
<div id="div5" style="display: none;">Stuff</div>
<div id="div6" style="display: none;">Stuff</div>
<div id="div7" style="display: none;">Stuff</div>
我需要一个 javascript 函数,它将可见元素更改加号或减号。只有一个应该可见。
这适用于所选答案。我对 localStorage 值感到头疼,直到我意识到我需要解析字符串。;)
这个例子没有使用jQuery,只是因为主题中没有jQuery-tag。(编辑,误解(
.HTML
<a href="" id="plus">+</a><a href="" id="minus">-</a>
<div class="plusable" style="display: block;">Stuff1</div>
<div class="plusable" style="display: none;">Stuff2</div>
<div class="plusable" style="display: none;">Stuff3</div>
<div class="plusable" style="display: none;">Stuff4</div>
<div class="plusable" style="display: none;">Stuff5</div>
<div class="plusable" style="display: none;">Stuff6</div>
<div class="plusable" style="display: none;">Stuff7</div>
.JS
window.onload = function () {
var plusButton = document.getElementById('plus');
var minusButton = document.getElementById('minus');
var plusables = document.getElementsByClassName('plusable');
var current = 0;
function hideCurrent () {
plusables[current].style.display = 'none';
}
function showCurrent () {
plusables[current].style.display = 'block';
}
plusButton.onclick = function () {
if (current === plusables.length - 1) {
return false;
}
hideCurrent();
current += 1;
showCurrent();
return false;
};
minusButton.onclick = function () {
if (current === 0) {
return false;
}
hideCurrent();
current -= 1;
showCurrent();
return false;
};
};
示例小提琴
尝试使用 jQuery:
$("div:visible").hide().next().show();
或
$("div:visible").hide().previous().show();
尝试一下,但没有测试,它使用 JQuery
function changeVisible(var divID, var operation)
{
//Get the new div id
var n=divID.split("div");
if (operation == "+")
var divNumber = parseInt('n[1]',10) + 1;
else if (operation == "-")
var divNumber = parseInt('n[1]',10) - 1;
var newDIV = "div"+divNumber;
//Using JQuery
$("#"+newDIV).css("display","block");
$("#"+divID).css("display","none");
}
> displayed
是当前显示的div 的编号,从 0 开始(因此div1
的数字为 0,div2
为 1 ...
howmany
表示您在循环中有多少div
var displayed = 0;
var howmany = 7;
function mod(a,b){
return ((a%b)+b)%b;
}
function f(amount){
var div = document.querySelector("#div"+(displayed + 1));
div.style.display = "none";
displayed = mod(displayed + amount , howmany);
var div = document.querySelector("#div"+(displayed + 1));
div.style.display = "block";
}
document.
querySelector("#addOne").
addEventListener("click", function () {f(1)});
document.
querySelector("#subOne").
addEventListener("click", function () {f(-1)});
http://jsfiddle.net/uSkq4/2/
相关文章:
- CKEditor Widget-阻止编辑可编辑元素本身
- 如何设置html元素填充的动画
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 使用clickToggle并在单击另一个元素时关闭元素
- 单击时将焦点更改为元素
- 如何在JavaScript中剥离数组元素中的非整数
- 函数来检查给定整数数组中给定位置的元素是否大于其两个邻域
- parseInt() 不会将数组的每个数组元素转换为整数
- 将 JSON 数组中的字符串元素解析为整数
- 向上/向下舍入公式的元素,直到结果为整数
- JavaScript 函数通过发送下一个元素的整数来更改元素的可见性
- 如何在javascript中找到整数数组中元素的和?(以下示例不起作用))
- jquery如何生成一个While,该While获取所有以String开头的元素并获取最后2个整数
- 在控制台中返回未定义的数组值/将数组元素舍入为整数
- 量角器计数元素并存储整数
- JavaScript -接受输入并将其显示为HTML元素,作为一个整数
- html元素的内容被解释为字符串,而不是整数
- 查找元素相对于视口或文档的整数像素位置:getBoundingClientRect
- 字符串解析——为什么在javascript中将输入数组元素全部解析为整数时会出现运行时错误