jquery切换多个ID
jquery toggle multiple id
我从w3school找到了一个示例程序,我设法对其进行了编辑。 但问题是,它太消耗代码了,我想要的是这段代码的简短版本。可能吗?(我试图将div 放入 php include 中,我使用单个切换函数和多个按钮,但一旦我单击一个按钮,所有div 都会显示)。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#p1").hide();
$("#p2").hide();
$("#p3").hide();
$("#p4").hide();
$("#button1").click(function(){
$("#p1").toggle();
});
$("#button2").click(function(){
$("#p2").toggle();
});
$("#button3").click(function(){
$("#p3").toggle();
});
$("#button4").click(function(){
$("#p4").toggle();
});
});
</script>
</head>
<body>
<div id="p1">
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
</div>
<button id="button1">Toggle</button>
<br>
<br>
<br>
<div id="p2">
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
</div>
<button id="button2">Toggle</button>
<br>
<br>
<br>
<div id="p3">
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
</div>
<button id="button3">Toggle</button>
<br>
<br>
<br>
<div id="p4">
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
</div>
<button id="button4">Toggle</button>
<br>
<br>
<br>
</body>
</html>
您可以先使用较短的脚本重新组织 HTML。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".msg").hide();
$(".toggle").click(function(){
$(this).parent( "div" ).find(".msg").toggle();
});
});
</script>
</head>
<body>
<div>
<div class="msg">
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
</div>
<button class="toggle">Toggle</button>
</div>
<br><br><br>
<div>
<div class="msg">
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
</div>
<button class="toggle">Toggle</button>
</div>
<br><br><br>
<div>
<div class="msg">
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
</div>
<button class="toggle">Toggle</button>
</div>
<br><br><br>
<div>
<div class="msg">
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
</div>
<button class="toggle">Toggle</button>
</div>
<br><br><br>
</body>
</html>
你可以像这样缩短你的代码,只要确保你为html元素添加正确的属性
$(document).ready(function() {
$(".toggleDiv").hide();
$(".btnToggle").click(function() {
var div = $(this).attr("data-div");
$("#" + div).toggle();
});
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
<body>
<div id="p1" class="toggleDiv">
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
</div>
<button id="button1" class="btnToggle" data-div="p1">Toggle</button>
<br>
<br>
<br>
<div id="p2" class="toggleDiv">
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
</div>
<button id="button2" class="btnToggle" data-div="p2">Toggle</button>
<br>
<br>
<br>
<div id="p3" class="toggleDiv">
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
</div>
<button id="button3" class="btnToggle" data-div="p3">Toggle</button>
<br>
<br>
<br>
<div id="p4" class="toggleDiv">
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
</div>
<button id="button4" class="btnToggle" data-div="p4">Toggle</button>
<br>
<br>
<br>
</body>
</html>
相关文章:
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 获取打开jquery对话框的button的id
- jQuery mobile not going through ID
- 如何将li类更改为li id's在Jquery中
- 如何使用jQuery迭代id名称的变体(如idname1、idname2)
- 获取不带Id JQuery的复选框值
- 从ID Jquery类获取返回值时出现问题
- attr() 不会更改 id - jquery
- 通过 ID jquery 限制文本区域字符
- 获取选定的行id jquery数据表行选择
- 如何在asp.net mvc中向Action方法发布简单ID(jquery-ajax)
- 添加文本到Javascript Span id jquery
- 动态获取控件ID - JQuery
- 验证选择字段取决于id jquery
- Increment id jquery
- 使用id jquery中的空格
- 根据HTML表的ID jQuery/javaScript对数据进行排序
- 以引号结束id - jquery
- 当draggable被拖放到可拖放区域时添加ID - jQuery Draggables
- 表格ID &jQuery中的名称