选择隐藏和显示问题:多于两个元素
Jquery Selecting to hide and show issue: More than two elements
最近尝试使工作页面显示和隐藏每个元素根据按钮,但我不知何故错过了一些东西,使这个工作,或不知何故它不工作。
当我运行这个例子时,前两个"A"链接工作,但不是第三个,是否有一种方法可以实际将其分离到每个元素显示单数而隐藏其余的地方?
下面是我的代码:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
<script>
$(document).ready(function(){
$("p").hide(); //to hide all P elem for user to start
$("#hide").click(function(){
$("p#2").show();
$("p#1").hide(); //Yes i tried grouping by selectors
$("p#3").hide(); //and also seperating like this
}); //but both didnt work :(
$("#show").click(function(){
$("p#1").show();
$("p#2").hide();
$("p#3").hide();
});
$("#other").click(function(){
$("p#3").show();
$("p#1").hide();
$("p#2").hide();
});
});
</script>
</head>
<body>
<div>please select a button to begin
<br />
<br />
<p id="1">If you click on the "Hide" button, I will disappear.</p>
<p id="2">If you click on the "Show" button, i will disappear!</p>
<p id="3">yay, another one</p>
<a id="hide">Hide</a>
<a id="show">Show</a>
<a id="third">other</a>
</div>
</body>
</html>
你需要替换这个:
$("#other").click(function(){
:
$("#third").click(function(){
$(document).ready(function(){
$("p").hide();
//to hide all P elem for user to start
$("#hide").click(function(){
$("p#2").show();
$("p#1").hide();
//Yes i tried grouping by selectors
$("p#3").hide();
//and also seperating like this
});
//but both didnt work :(
$("#show").click(function(){
$("p#1").show();
$("p#2").hide();
$("p#3").hide();
});
$("#third").click(function(){
$("p#3").show();
$("p#1").hide();
$("p#2").hide();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
<div>please select a button to begin
<br />
<br />
<p id="1">If you click on the "Hide" button, I will disappear.</p>
<p id="2">If you click on the "Show" button, i will disappear!</p>
<p id="3">yay, another one</p>
<a id="hide">Hide</a>
<a id="show">Show</a>
<a id="third">other</a>
</div>
相关文章:
- 如何在jQuery中包装两个元素的组
- 无法使以下ng on click outside指令在同一页面上的两个元素上工作
- 检查来自不同数组的两个元素的一个属性是否相等
- javascript函数包含两个元素和web音频api
- 如何将日期和时间拆分为两个元素
- 检查最后两个元素
- 插件总是只得到最后一个实例,如何让它适用于多个元素
- 在两个元素的 onrender 事件之后执行函数
- 在照片库中的两个元素之间切换样式会发生变化
- 将单个服务逻辑应用于两个类似的按钮
- 区分html代码中的两个元素
- 如何将相同的功能应用于两个按钮
- 如何使用jQuery在两个元素之间切换类
- 如何使菜单容器在悬停两个元素以外的所有元素时隐藏
- 单击页面上的任意位置时隐藏两个元素
- Jquery两个元素,切换焦点/点击..有效但滞后
- 自动完成不会't适用于具有相同id的两个元素
- jQuery UI相对于两个元素的位置
- 聚合物.纸质子菜单触发器作用于纸质项目中的两个元素
- 如何将函数应用于两个元素