让我们开始吧!
我做了一个 PHP 计算,但不想在我的网站上显示。棘手的部分是我希望显示值,每次用户单击按钮时 ONCE 值 ONE 值。不管它是什么类型的按钮,我只是不想在每次点击时都显示一个值。值的总数是 8(固定长度),现在我有 4 个数组,每个数组有 2 个值。所以每件事都完成并保存了,我只是想展示它,一次一个。如果它更容易,我可以将它们组合成 1 个数组。我试图制作一种 display:none
vs display onclick
东西,但它只显示每次点击的第一个值。非常感谢提示和技巧!一种 FORM
可以帮助我吗?通过使用 input
代替?
这是我得到的:
HTML
<a href="#" class="modern" onClick="showhide('first', 'block'); return false" >Give me your value</a> <div id="first"><?php echo $team1[0];?></div> <div class="secound"><?php echo $team1[1];?></div> <div class="third"><?php echo $team2[0];?></div> <div class="fourth"><?php echo $team2[1];?></div> ...
Javascript
function showhide(divid, state){
document.getElementById(divid).style.display=state
}
...
最佳答案
有很多不同的方法可以做到这一点。使用框架会让您更轻松、更轻松地支持多种浏览器,但假设这不是目标并尽可能多地坚持您所拥有的...
我对 PHP 做了一些改动:
<a href="#" id="team-next">Give me your value</a>
<div id="teams">
<div id="team-0"><?= $team[0][0] ?></div>
<div id="team-1"><?= $team[0][1] ?></div>
<div id="team-2"><?= $team[1][0] ?></div>
<div id="team-3"><?= $team[1][1] ?></div>
...
请注意,我已将 id 更改为前缀 team-
和索引 0 ... n,并且团队出现在具有 id teams 的父元素中。我还将数组更改为多维数组,而不是其中包含数字的多个变量。可以通过将数组添加为数组的一项来创建此构造。现在是 JavaScript(这应该出现在上述 HTML 之后的脚本标记中):
// Initial index -1 (nothing displayed)
var current = -1;
// How many teams are there?
var count = document.getElementById("teams").children.length;
// The handler
function showNext() {
// A bit of modulus math, when it gets to the
// same value as the count it starts at 0 again
var next = (current + 1) % count;
// Hide the previous one, unless it is the first
if(current !== -1)
document.getElementById("team-" + current).removeAttribute("style");
// Show the next one
document.getElementById("team-" + next).style.display = "block";
// Update the current index
current = next;
}
// Bind the event listener
document.getElementById("team-next").addEventListener("click", showNext, false);
关于php - 单击 PHP 时显示数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16633454/