我如何制作显示/隐藏部分的列表
How can I make a list of show/hide sections?
对于我的网站,我正在制作显示/隐藏链接(java脚本和css)的列表,我希望每个人都能说些不同的东西。然而,当我这样做的时候,第一个链接之后的显示/隐藏链接不起作用。任何帮助吗?建议吗?谢谢!
function showHide(shID) {
if (document.getElementById(shID)) {
if (document.getElementById(shID+'-show').style.display != 'none') {
document.getElementById(shID+'-show').style.display = 'none';
document.getElementById(shID).style.display = 'block';
}
else {
document.getElementById(shID+'-show').style.display = 'inline';
document.getElementById(shID).style.display = 'none';
}
}
}
.more {
display: none;
a.showLink, a.hideLink {
text-decoration: none;
color: #36f;
padding-left: 8px;
background: transparent url(down.gif) no-repeat left; }
a.hideLink {
background: transparent url(up.gif) no-repeat left; }
<a href="#" id="example-show" class="showLink" onclick="showHide('example');return false;"><span class="sec">Section 1-</span><span class="seccontent">site navigation</span></a>
<div id="example" class="more">
<a href="#" id="example-hide" class="hideLink" onclick="showHide('example');return false;"><span class="sec">Section 1-</span><span class="seccontent">site navigation</span></a>
<p>example content</p>
</div>
<a href="#" id="example-show" class="showLink" onclick="showHide('example');return false;"><span class="sec">Section 2-</span><span class="seccontent">products</span></a>
<div id="example" class="more">
<a href="#" id="example-hide" class="hideLink" onclick="showHide('example');return false;"><span class="sec">Section 2-</span><span class="seccontent">products</span></a>
<p>stuffs</p>
</div>
不能使用相同id的两个元素
试着用"Sec1"和第二个用"Sec2"
<a href="#" id="example-show" class="showLink" onclick="showHide('Sec1');return false;"><span class="sec">Section 1-</span><span class="seccontent">site navigation</span></a>
<div id="Sec1" class="more">
<a href="#" id="example-hide" class="hideLink" onclick="showHide('Sec1');return false;"><span class="sec">Section 1-</span><span class="seccontent">site navigation</span></a>
<p>example content</p>
</div>
<a href="#" id="A1" class="showLink" onclick="showHide('Sec2');return false;"><span class="sec">Section 2-</span><span class="seccontent">products</span></a>
<div id="Sec2" class="more">
<a href="#" id="A2" class="hideLink" onclick="showHide('Sec2');return false;"><span class="sec">Section 2-</span><span class="seccontent">products</span></a>
<p>stuffs</p>
</div>
如果你使用jQuery,那么试试toggleClass。
<html>
<head>
<style>
div {
display: none;
}
div.active {
display: block;
}
</style>
</head>
<body>
<span id="clickMe">Click</span>
<div>Hello World</div>
<script>
$(document).ready(function () {
$('#clickMe').on('click', function () {
$("div").toggleClass("active");
});
});
</script>
</body>
</html>
演示为了有相同的按钮,只是改变文本,你可以应用这个脚本:
$(document).ready(function () {
$('#hide').on('click', function () {
if ($('#par').is(':visible')) {
$(this).text('Write me');
$('#par').hide();
} else {
$(this).text("I don't want to write any message now.");
$('#par').show();
}
});
});演示
相关文章:
- 如何使用javascript将值引导下拉列表设置为隐藏值asp.net
- 当链接单击-jQuery时,第二层下拉列表将隐藏
- 修复列表的绝对位置(li's隐藏在前两个后面)
- 如何使用jQuery隐藏与选择选项相关的选择选项列表
- 使用2个下拉列表显示/隐藏分区
- 选中复选框时如何显示/隐藏下拉列表
- 如何在 *ngFor 列表中显示单击的元素,使用 Angular 2 隐藏其他元素
- 从未隐藏的列表项中获取项
- HTML 表单字段根据从下拉列表中选择的选项显示/隐藏
- 根据下拉列表中选择的项目隐藏所有元素
- 隐藏并显示列表中的元素
- 删除或隐藏下拉列表中的一些值,该列表由使用JavaScript的存储过程填充
- 如何使用jQuery显示()和隐藏()列表项块
- 如何从另一个下拉列表中隐藏选择基于下拉列表的值
- 如何部分隐藏asp.net复选框列表项
- 以父对象为中心并带有溢出的下拉列表:隐藏
- 引导程序选择下拉列表被另一个下拉列表隐藏
- 动态列表隐藏空元素
- 如何根据另一个下拉列表隐藏和显示下拉列表值
- 如何使嵌套列表隐藏类似于jquery移动.示例包括在内