我有多个使用此 javascript 切换的 div
<script language="JavaScript">
//here you place the ids of every element you want.
var ids = new Array('a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10');
function switchid(id) {
hideallids();
showdiv(id);
}
function hideallids() {
//loop through the array and hide each element by id
for (var i = 0; i < ids.length; i++) {
hidediv(ids[i]);
}
}
function hidediv(id) {
//safe function to hide an element with a specified id
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById(id).style.display = 'none';
}
else {
if (document.layers) { // Netscape 4
document.id.display = 'none';
}
else { // IE 4
document.all.id.style.display = 'none';
}
}
}
function showdiv(id) {
//safe function to show an element with a specified id
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById(id).style.display = 'block';
}
else {
if (document.layers) { // Netscape 4
document.id.display = 'block';
}
else { // IE 4
document.all.id.style.display = 'block';
}
}
}
</script>
然后每个 div 都有一些选项(是、否、继续等)从一个 div 切换到另一个
<div id='a1' style="display:block;">
<div style="border-bottom: 1px solid black;margin-left: auto; margin-right: auto;text-align: center;">
<i>"I'm sorry to hear you have lost your card. I can cancel the lost card for your proection."</i><br />
<b>Find the most recent card by checking it's ISSUE date.</b><br /><br />
</div>
<div style="margin-left: auto; margin-right: auto;text-align: center;">
<span style="color:red;">Has the card been deactivated already?</span><br /><br />
<a class="spl_btn btn_green" href="javascript:switchid('a2');" onclick="document.getElementById('back').href='javascript:switchid(\'a1\');';"><span><span>Yes</span></span></a>
<a class="spl_btn btn_pink" href="javascript:switchid('a3');" onclick="document.getElementById('back').href='javascript:switchid(\'a1\');';"><span><span>No</span></span></a>
</div>
</div>
然后在切换 div 之外我有一个后退按钮。如果您在选择一个选项时可以从上面看到,它会更改为相应的 div,然后切换后退按钮的 href 值。现在我唯一的问题是我有 2 个 div(数字 3 和 4),它们都有一个选项(在这种情况下没有)导致 5 或 6。这个特定的按钮我在一个 php 变量中像这样
<?php echo $addr_update; ?>
然后他们自己的变量像这样工作
$project_id = $_GET["project_id"];
switch ($project_id) {
case "al":
$addr_update = "<a class=\"spl_btn btn_pink\" href=\"javascript:switchid('a5');\"><span><span>No</span></span></a>";
break;
所以这个按钮有 32 个不同的变量。我希望能够将 onclick 事件添加到可能让它获取其 div id 的变量中,然后相应地更改后退按钮。
例如,您在 div 2 上并按 no,在这种情况下将引导您到 div 5 而不是 div 6。后退按钮需要指向 div 2。因为 div 2 和 div 3 都可以导致 5 或 6根据变量,我不能只添加 onclick 事件来更改后退按钮而不知道用户以前在哪里。
编辑:只是添加一点。如果您单击 DIV 2 上的选项,那么它会将后退按钮设置为 DIV 2。所以在我看来,如果我可以获取 DIV ID 然后将其应用于它的href="javascript:switchid('a4');"
一部分,它应该很简单。有点像这样:
<a class="spl_btn btn_green" href="javascript:switchid('a2');" **onclick="document.getElementById('back').href='javascript:switchid(\'a1\');'; Some sort of function that would grab the containing divs id and then put it in the .href value of the document.getElement part**"><span><span>Yes</span></span></a>
希望这是有道理的,任何帮助将不胜感激。