[英]hide show not working on multiple divs
I am using the slideToggle method to hide/show a div. 我正在使用slideToggle方法隐藏/显示div。 I am trying to replicate this action multiple times on a page but it only works on the 1st one.
我试图在一个页面上多次复制此操作,但仅在第一个页面上有效。 I figured out a way to have it work on each div but have to rename each div and add a new function in the javascript.
我想出了一种方法可以在每个div上工作,但必须重命名每个div并在javascript中添加新功能。 What can I do to clean up my code as I am going to be using this through out the page and be creating new instances for each div does is not efficient.
我将要在整个页面中使用该代码并为每个div创建新实例,这对我清理代码是无效的。
<script>
$(document).ready(function() {
$("#flip").click(function() {
$("#panel").slideToggle("slow");
});
$("#flip2").click(function() {
$("#panel2").slideToggle("slow");
});
$("#flip3").click(function() {
$("#panel3").slideToggle("slow");
});
});
</script>
<p class="button-label">Active</p>
<button type="button" class="btn a-cart"><span>ADD TO CART</span></button>
<div id="flip">Click to view HTML</div>
<div id="panel" style="display:none">
<textarea rows="3" cols="50">
<button type="button" class="btn a-cart"><span>ADD TO CART</span></button>
</textarea>
</div>
<p class="button-label">Disabled</p>
<button type="button" class="btn a-cart disabled"><span>ADD TO CART</span></button>
<div id="flip2">Click to view HTML</div>
<div id="panel2" style="display:none">
<textarea rows="3" cols="50">
<button type="button" class="btn a-cart disabled"><span>ADD TO CART</span></button>
</textarea>
</div>
<p class="button-label">Select Options</p>
<button type="button" class="btn select-options"><span>select options</span></button>
<div id="flip3">Click to view HTML</div>
<div id="panel3" style="display:none">
<textarea rows="3" cols="50">
<button type="button" class="btn select-options"><span>select options</span></button>
</textarea>
</div>
You can use classes instead of ids and use .next()
method in callback function: 您可以使用类而不是id并在回调函数中使用
.next()
方法:
$(document).ready(function(){ $(".flip").click(function(){ $(this).next('.panel').slideToggle("slow"); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p class="button-label">Active</p> <button type="button" class="btn a-cart"><span>ADD TO CART</span></button> <div class="flip">Click to view HTML</div> <div class="panel" style="display:none"> <textarea rows="3" cols="50"><button type="button" class="btn a-cart"><span>ADD TO CART</span></button> </textarea> </div> <p class="button-label">Disabled</p> <button type="button" class="btn a-cart disabled"><span>ADD TO CART</span></button> <div class="flip">Click to view HTML</div> <div class="panel" style="display:none"> <textarea rows="3" cols="50"><button type="button" class="btn a-cart disabled"><span>ADD TO CART</span> </button> </textarea> </div> <p class="button-label">Select Options</p> <button type="button" class="btn select-options"><span>select options</span></button> <div class="flip">Click to view HTML</div> <div class="panel" style="display:none"> <textarea rows="3" cols="50"><button type="button" class="btn select-options"><span>select options</span> </button> </textarea> </div>
You need not to replicate this and rename <div>
and create new function. 您无需复制它并重命名
<div>
并创建新功能。
Take these steps: 请执行以下步骤:
$(this)
to identify clicked element. $(this)
标识单击的元素。 .next()
function to get the .next element. .next()
函数获取.next元素。 .slideToggle()
on that. .slideToggle()
。 See the code below: 请参见下面的代码:
<script>
$(document).ready(function() {
$(".flip").click(function() {
$(this).next().slideToggle("slow");
});
});
</script>
<p class="button-label">Active</p>
<button type="button" class="btn a-cart"><span>ADD TO CART</span></button>
<div class="flip">Click to view HTML</div>
<div id="panel" style="display:none">
<textarea rows="3" cols="50">
<button type="button" class="btn a-cart"><span>ADD TO CART</span> </button>
</textarea>
</div>
<p class="button-label">Disabled</p>
<button type="button" class="btn a-cart disabled"><span>ADD TO CART</span></button>
<div class="flip">Click to view HTML</div>
<div id="panel2" style="display:none">
<textarea rows="3" cols="50">
<button type="button" class="btn a-cart disabled"><span>ADD TO CART</span></button>
</textarea>
</div>
<p class="button-label">Select Options</p>
<button type="button" class="btn select-options"><span>select options</span></button>
<div class="flip">Click to view HTML</div>
<div id="panel3" style="display:none">
<textarea rows="3" cols="50">
<button type="button" class="btn select-options"><span>select options</span></button>
</textarea>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.