【发布时间】:2014-04-10 21:56:38
【问题描述】:
我能够使用下面的代码块实现内容切换器,但我正在寻找一种简化它的方法。最多有 10 个或更多主题可供切换,我如何简化它以使代码不会太大,而不是每个 DIV 有一个代码块。
jQuery(document) .ready(function () {
$('.topic-intro:not(:nth-of-type(1))') .hide();
$('#mid-nav-in ul li:nth-of-type(1)') .addClass('active');
$('#mid-nav-in ul li a:nth-of-type(1)') .click(function () {
$('.topic-intro:not(:nth-of-type(1))') .hide();
$('.topic-intro:nth-of-type(1)') .show();
$('#mid-nav-in ul li:not(:nth-of-type(1))') .removeClass('active');
$('#mid-nav-in ul li:nth-of-type(1)') .addClass('active');
});
});
jQuery(document) .ready(function () {
$('#mid-nav-in ul li:nth-of-type(2) a') .click(function () {
$('.topic-intro:not(:nth-of-type(2))') .hide();
$('.topic-intro:nth-of-type(2)') .show();
$('#mid-nav-in ul li:nth-of-type(2)') .addClass('active');
$('#mid-nav-in ul li:not(:nth-of-type(2))') .removeClass('active');
});
});
【问题讨论】:
-
你能用一个简化的例子做一个 jsfiddle 吗?如果没有与脚本相关的标记,这很难解码。
标签: javascript jquery performance jquery-ui