[英]jQuery different div ids show/hide
I am no coder, still learning.. 我不是程序员,还在学习。
Have number of div
s listed by mysql query. 有mysql查询列出的
div
数量。 I am in need of a javascript code that can show the class="qform"
div depending on which button is clicked. 我需要一个JavaScript代码,该代码可以显示
class="qform"
div,具体取决于单击的按钮。
if button-1
is clicked, then show the class qform
piece in the same div
( id="tour-1"
) 如果
button-1
,则在同一div
显示类qform
片段( id="tour-1"
)
I could get the first div working, but there is no way I can duplicate the code for every possible number of div
outputs. 我可以使第一个div工作,但是我无法为每个可能的
div
输出数量重复代码。
is there a solution to my problem? 我的问题有解决方案吗?
<script>
$(document).ready( function(){
$("#button-1").click(function () {
$(".qform").slideToggle("fast");
$("#book-1.book" ).toggleClass( "book-off" );
}); });
</script>
<style>
.qform {display:none;}
</style>
<div id="tour-1" class="tour">
some text
<div class="qform">Full name, email x2, tour name 1</div>
<div id="book-1" class="book"><a id="button-1" href="JavaScript:void()">book now</a></div>
some text
</div>
<div id="tour-2" class="tour">
some text
<div class="qform">Full name, email x2,tour name 2</div>
<div id="book-2" class="book"><a id="button-2" href="JavaScript:void()">book now</a></div>
some text
</div>
<div id="tour-3" class="tour">
some text
<div class="qform">Full name, email x2, tour name 3</div>
<div id="book-3" class="book"><a id="button-3" href="JavaScript:void()">book now</a></div>
some text
</div>
<div id="tour-4" class="tour">
some text
<div class="qform">Full name, email x2, tour name 4</div>
<div id="book-4" class="book"><a id="button-4" href="JavaScript:void()">book now</a></div>
some text
</div>
You can use .parent()
to walk up the DOM and then call functions on the respective elements: 您可以使用
.parent()
遍历DOM,然后在各个元素上调用函数:
$(document).ready( function(){
$("a").click(function () {
$(this).parent().parent().find(".qform").slideToggle("fast");
$(this).parent().parent().find(".book" ).toggleClass( "book-off" );
});
});
jsFiddle: http://jsfiddle.net/fastfieros/3EcDn/ jsFiddle: http : //jsfiddle.net/fastfieros/3EcDn/
Take a look at: http://jsfiddle.net/stackolee/VpgfK/2/ 看看: http : //jsfiddle.net/stackolee/VpgfK/2/
It would be easier if you gave the anchor tags a class, as this will only work if there aren't any additional anchors in the tour div. 如果为锚标记提供一个类会更容易,因为这仅在游览div中没有任何其他锚的情况下才有效。
Also, retire the <a href="Javascript:void()">
stuff. 另外,退出
<a href="Javascript:void()">
内容。 Returning 'false' from a jquery click function has the same effect. 从jquery click函数返回“ false”具有相同的效果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.