JQuery 显示隐藏菜单中的多个 DIV 以及 DIV 内的菜单
我是 JQuery 新手。我正在尝试显示和隐藏多个 div。我有一个显示 A、B、C 的菜单。当我单击 A 时,会出现 div A1。 Div A1 中有一个菜单,显示 1、2、3 等。当我单击此菜单中的 2 时,A1 消失并出现 A2。当我单击 B 时,当前可见的 div 消失并出现 B1。 B1 有一个显示 1、2、3 的菜单。单击 2 时,B1 被 B2 替换。等等——我想你已经明白了。
这是我的 html:
<div class="thework"><a href="#" class="A">A</a> <a href="#" class="B">B</a> <a href="#" class="C">C</a></div>
<div class="work-A-link1">A1<br />
<a href="#" class="link1">1</a> <a href="#" class="link2">2</a> <a href="#" class="link3">3</a>
</div>
<div class="work-A-link2">A2<br />
<a href="#" class="link1">1</a> <a href="#" class="link2">2</a> <a href="#" class="link3">3</a>
</div>
<div class="work-A-link3">A3<br />
<a href="#" class="link1">1</a> <a href="#" class="link2">2</a> <a href="#" class="link3">3</a>
</div>
<div class="work-B-link1">B1<br />
<a href="#" class="link1">1</a> <a href="#" class="link2">2</a> <a href="#" class="link3">3</a>
</div>
<div class="work-B-link2">B2<br />
<a href="#" class="link1">1</a> <a href="#" class="link2">2</a> <a href="#" class="link3">3</a>
</div>
<div class="work-B-link3">B3<br />
<a href="#" class="link1">1</a> <a href="#" class="link2">2</a> <a href="#" class="link3">3</a>
</div>
<div class="work-C-link1">C1<br />
<a href="#" class="link1">1</a> <a href="#" class="link2">2</a> <a href="#" class="link3">3</a>
</div>
<div class="work-C-link2">C2<br />
<a href="#" class="link1">1</a> <a href="#" class="link2">2</a> <a href="#" class="link3">3</a>
</div>
<div class="work-C-link3">C3<br />
<a href="#" class="link1">1</a> <a href="#" class="link2">2</a> <a href="#" class="link3">3</a>
</div>
这是与之配套的代码:
$(document).ready(function() {
$('div[class^=work-]').hide();
$(".A").click(function() {
$("div[class^=work-]").hide();
$("div[class^=work-A-link1]").fadeIn();
});
$(".B").click(function() {
$("div[class^=work-]").hide();
$("div[class^=work-B-link1]").fadeIn();
});
$(".C").click(function() {
$("div[class^=work-]").hide();
$("div[class^=work-C-link1]").fadeIn();
});
$('[class^=link]').click(function() {
$('div[class^=work-]').hide();
var $this = $(this);
var x = $this.attr("className");
$(".work-A-" + x).fadeIn();
return false;
});
});
该代码仅适用于 div“A”,并且我必须单独对每个 div 进行编号,这似乎效率很低。我尝试过使用 split() 来收集类名并定位相应的 div,但我没有编码技能。有一个优雅的解决方案吗?
I'm new to JQuery. I'm trying to show and hide multiple divs. I have a menu showing A,B,C. When I click A, the div A1 appears. Div A1 has a menu within it showing 1, 2, 3 and so on. When I click 2 in this menu, A1 disappears and A2 appears. When I click B, the currently visible div disappears and B1 appears. B1 has a menu showing 1, 2, 3. When you click 2, B1 is replaced by B2. And so on - I think you get the picture.
Here is my html:
<div class="thework"><a href="#" class="A">A</a> <a href="#" class="B">B</a> <a href="#" class="C">C</a></div>
<div class="work-A-link1">A1<br />
<a href="#" class="link1">1</a> <a href="#" class="link2">2</a> <a href="#" class="link3">3</a>
</div>
<div class="work-A-link2">A2<br />
<a href="#" class="link1">1</a> <a href="#" class="link2">2</a> <a href="#" class="link3">3</a>
</div>
<div class="work-A-link3">A3<br />
<a href="#" class="link1">1</a> <a href="#" class="link2">2</a> <a href="#" class="link3">3</a>
</div>
<div class="work-B-link1">B1<br />
<a href="#" class="link1">1</a> <a href="#" class="link2">2</a> <a href="#" class="link3">3</a>
</div>
<div class="work-B-link2">B2<br />
<a href="#" class="link1">1</a> <a href="#" class="link2">2</a> <a href="#" class="link3">3</a>
</div>
<div class="work-B-link3">B3<br />
<a href="#" class="link1">1</a> <a href="#" class="link2">2</a> <a href="#" class="link3">3</a>
</div>
<div class="work-C-link1">C1<br />
<a href="#" class="link1">1</a> <a href="#" class="link2">2</a> <a href="#" class="link3">3</a>
</div>
<div class="work-C-link2">C2<br />
<a href="#" class="link1">1</a> <a href="#" class="link2">2</a> <a href="#" class="link3">3</a>
</div>
<div class="work-C-link3">C3<br />
<a href="#" class="link1">1</a> <a href="#" class="link2">2</a> <a href="#" class="link3">3</a>
</div>
And here's the code to go with it:
$(document).ready(function() {
$('div[class^=work-]').hide();
$(".A").click(function() {
$("div[class^=work-]").hide();
$("div[class^=work-A-link1]").fadeIn();
});
$(".B").click(function() {
$("div[class^=work-]").hide();
$("div[class^=work-B-link1]").fadeIn();
});
$(".C").click(function() {
$("div[class^=work-]").hide();
$("div[class^=work-C-link1]").fadeIn();
});
$('[class^=link]').click(function() {
$('div[class^=work-]').hide();
var $this = $(this);
var x = $this.attr("className");
$(".work-A-" + x).fadeIn();
return false;
});
});
This code only works with the div "A", and I'd have to number each div individually, which seems really inefficient. I've tried using split() to gather up the class names and target the respective divs, but I don't have the coding skills. Is there an elegant solution to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你可以在 HTML 中拆分类,我有不同的方法?
例如,div
您可以将它们更改为吗?
如果是这样,这似乎可以工作..
稍后将添加一个小提琴添加小提琴工作示例:这里
I have a different way if you can split up the classes in your HTML?
e.g. the divs which are
<div class="work-A-link1">
can you change them to?
<div class="work A link1">
if so this appears to work..
will add a fiddle a bit lateradded fiddleWorking Example : Here
你可以这样做:
希望这有帮助。干杯
You could do it like this:
Hope this helps. Cheers