单击此按钮,隐藏信息并在JavaScript中加载新信息
Click this button, hide info and load new info in JavaScript
https://jsfiddle.net/tsquinn/p044mb36/
这是我尝试做的示例html
<ul class="pageMenu">
<li class="colorLink"><a href="">Red</a></li>
<li class="colorLink"><a href="">Blue</a></li>
<li class="colorLink"><a href="">Green</a></li>
<li class="colorLink"><a href="">Purple</a></li>
</ul>
<section id="contents">
<h1>Contents</h1>
<p>
This is where the colored text loads. Each ".colorLink" click must overwrite the text that's here with the new text that corresponds with the link clicked.
</p>
</section>
<div id="colors">
<div class="red">
<h2>Red</h2>
<p>
This is RED text!
</p>
</div>
<div class="blue">
<h2>Blue</h2>
<p>
This is BLUE text!
</p>
</div>
<div class="green">
<h2>Green</h2>
<p>
This is GREEN text!
</p>
</div>
<div class="purple">
<h2>Purple</h2>
<p>
This is PURPLE text!
</p>
</div>
</div>
问题是:加载页面时,我需要隐藏div#colors
。然后单击链接将相应的文本加载到页面的内容部分。
我在这里和网上的其他地方查看了答案。我发现了一个问答;允许按下按钮显示/隐藏子链接,但我的链接不是子链接。我对JS知之甚少,但即使使用我找到的代码也无法解决问题。我可以使用按钮来切换相应的文本,但我无法从一开始就隐藏所有内容。
这是我所能做到的,我知道这是不对的:
<script>
var removeText = document.getElementById('colors').style.display='none';
$(document).ready(function showLink(){
removeText;
$('.colorLink a').click(function(e){
e.preventDefault();
var links = document.getElementsByName('ul.pageMenu li');
var page = document.getElementsByClassName('#colors div');
if ($(this).links == page) {
$("#contents")
}
});
});
</script>
这里发生了一些事情。
首先,对document.ready()
的回调通常是匿名的,就像在您的点击处理程序中一样:
$(document).ready(function() {...});
那么,links
和page
的值是不同元素的不同集合,所以$(this).links == page
永远不会是true
。
最后,您要混合一些纯javascript(document.getElement...
(和jquery $(...)
,这会让您头疼。
相反,你可以做一些类似(未经测试(的事情:
$(document).ready(function(){
$('#colors').hide();
$('.colorLink a').click(function(e){
e.preventDefault();
// re-hide the divs
$('#colors').hide();
// get the text of the target of the click
var link = $(e.target).text().toLowerCase();
// show the corresponding div
$('#colors div#'+link).show();
});
});
您可以通过获取索引来实现。推荐正在工作的演示。希望这对你有帮助。
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<style type="text/css">
div#colors div{
display: none;
}
</style>
<body>
<ul class="pageMenu">
<li class="colorLink"><a href="">Red</a></li>
<li class="colorLink"><a href="">Blue</a></li>
<li class="colorLink"><a href="">Green</a></li>
<li class="colorLink"><a href="">Purple</a></li>
</ul>
<div id="colors">
<div class="red">
<h2>Red</h2>
<p>
This is RED text!
</p>
</div>
<div class="blue">
<h2>Blue</h2>
<p>
This is BLUE text!
</p>
</div>
<div class="green">
<h2>Green</h2>
<p>
This is GREEN text!
</p>
</div>
<div class="purple">
<h2>Purple</h2>
<p>
This is PURPLE text!
</p>
</div>
</div>
<script type="text/javascript">
$("ul.pageMenu li").click(function(e){
e.preventDefault();//stop the reloading page
var theindex = $(this).index();//get the index number of clicked li
//alert(theindex);
$("div#colors div").eq(theindex).slideToggle(500);//according to the clicked id show/hide div.
});
</script>
</body>
</html>
所以我修改了@Anuradh S提供的代码,并想出了这个代码,几乎完全得到了我想要的。
$("ul.pageMenu li").click(function(e){
e.preventDefault();//stop the reloading page
var theindex = $(this).index();//get the index number of clicked li
//alert(theindex);
$("#contents").hide(300);
$("div#colors div").hide(400);
$("div#colors div").eq(theindex).slideToggle(500);//according to the clicked id show/hide div.
});
最后一个想法是防止当前文本链接再次被点击,或者阻止文本重新加载。
再次感谢!
相关文章:
- JS表单提交"无法使用Chrome数据保护程序加载此页面.尝试重新加载页面.调试信息:POST CISmtuK
- jQuery:获取图像加载错误的详细信息
- SuiteScript:如何有一个自定义的数据字段来从数据库加载信息
- 无法让 ajax 使用 PHP 从 MySQL 加载信息表
- 无法将数据重新加载到具有来自新nurl的附加信息的DataTable中
- 加载信息中的指令
- 点击其他页面加载信息
- 单击按钮时如何将信息加载到另一个页面
- 谷歌自动完成 - 如何获取有关加载地址的信息
- 将加载指示器添加到谷歌地图信息窗口
- 创建可重复的 HTML 表单并从 JS 数组加载信息
- 获取加载 ajax 的文档信息
- 谷歌地图信息窗口未从变量加载内容
- 需要模块的JS加载超时:错误详细信息
- 用于调查的 Javascript:从页面加载时从出生日期开始计算年龄 从预填充信息加载
- 在加载了ngmap的地图上显示信息窗口
- 谷歌地图信息窗口 ajax 加载内容
- 通过 JavaScript 重新加载页面,重新发送表单信息
- 将信息发送到数据库,而无需重新加载页面
- 如何将本地文本信息加载到