切换同一个班级的一个div

编程入门 行业动态 更新时间:2024-10-24 10:14:43
本文介绍了切换同一个班级的一个div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当几个div与jQuery具有相同的类时,如何使用 Toggle ?我只想在点击时显示一个div.

JavaScript

$(".help_content").hide(); $(".help_target").click(function() { $('.help_content').toggle(); // I also tried with $(".help_content").show(); });

HTML

<div id="foo"> <p class="help_target"> <a href="#">Click</a> </p> </div> <p class="help_content">Asia</p> <div id="some"> <p class="help_target"> <a href="#">Click</a> </p> </div> <p class="help_content">Africa</p>

我不能使用 next(),因为.help_content不是.help_target的后代. (我想在字段集中使用.help_target并在字段集中显示.help_content.)

解决方案

您可以执行以下操作:

$(".help_content").hide(); $(".help_target").click(function() { $(this).parent().next('.help_content').toggle(); });

查看实际情况: jsfiddle/mattball/X7p28/

How to use Toggle when several div have the same class with jQuery? I want to show just one div on click.

JavaScript

$(".help_content").hide(); $(".help_target").click(function() { $('.help_content').toggle(); // I also tried with $(".help_content").show(); });

HTML

<div id="foo"> <p class="help_target"> <a href="#">Click</a> </p> </div> <p class="help_content">Asia</p> <div id="some"> <p class="help_target"> <a href="#">Click</a> </p> </div> <p class="help_content">Africa</p>

I can't use next() since .help_content is not a descendant of .help_target. (I want to use .help_target in fieldsets and display .help_content out of fieldsets).

解决方案

You can do this:

$(".help_content").hide(); $(".help_target").click(function() { $(this).parent().next('.help_content').toggle(); });

See it in action: jsfiddle/mattball/X7p28/

更多推荐

切换同一个班级的一个div

本文发布于:2023-10-31 05:57:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545212.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:班级   div

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!