帮助优化一些 jQuery 功能
我有一些简单的 jQuery,添加了一些选项卡功能。这是基于我在某个地方找到的东西(不记得在哪里),但目前它可能大部分是原创的。我在使用某些选择器时遇到了麻烦,因此我不得不做一些看起来很尴尬的事情(请参阅 .children().children 部分)。尴尬的部分源于添加处理单个页面上的多组选项卡的功能。
我可能使用错误的选择器吗?您还能看到这里还有其他改进的空间吗?
这是 jQuery:
// Create and manage tab blocks
$('.tab-sections section').hide(); // Hide all tabs
$('.tab-list').each(function(){ // Make the first tab active
$('.tab-list li:first-child').addClass('active');
});
$('.tab-sections').each(function(){ // Make the first tab's content visible
$('.tab-sections section:first-child').show();
});
$('.tab-list li').click(function() {
$(this).siblings().removeClass('active'); // Remove any "active" class
$(this).addClass('active'); // Add "active" class to selected tab
// Hide tabs. This looks awkward, but .children('.tab-sections section') wouldn't work
$(this).parents('.tab-block').children('.tab-sections').children('section').hide();
// Find the href attribute value to identify the active tab + content
var activeTab = $(this).find('a').attr('href');
$(activeTab).fadeIn('fast'); // Fade in the active ID content
return false;
});
这是 HTML:
<div class="tab-block grad-tabs">
<ul class="tab-list">
<li><a href="#tab1" title="tab 1">Tab 1</a></li>
<li><a href="#tab2" title="tab 2">Tab 2</a></li>
<li><a href="tab3" title="tab 3">Tab 3</a></li>
</ul>
<div class="tab-sections">
<section id="tab1">
<h2>Tab 1</h2>
<p>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</section>
<section id="tab2">
<h2>Tab 2</h2>
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
</section>
<section id="tab3">
<h2>Tab 3</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</section>
</div>
</div>
I have some simple jQuery that adds some tab functionality. This is based on something I found somewhere (can't recall where), but it's probably mostly original at this point. I was having trouble with some selectors, so I had to do some stuff that seemed awkward (see the .children().children part). The awkward parts stem from adding the ability to handle multiple sets of tabs on a single page.
Am I possibly using the selector wrong? And can you see any other room for improvement here?
Here's the jQuery:
// Create and manage tab blocks
$('.tab-sections section').hide(); // Hide all tabs
$('.tab-list').each(function(){ // Make the first tab active
$('.tab-list li:first-child').addClass('active');
});
$('.tab-sections').each(function(){ // Make the first tab's content visible
$('.tab-sections section:first-child').show();
});
$('.tab-list li').click(function() {
$(this).siblings().removeClass('active'); // Remove any "active" class
$(this).addClass('active'); // Add "active" class to selected tab
// Hide tabs. This looks awkward, but .children('.tab-sections section') wouldn't work
$(this).parents('.tab-block').children('.tab-sections').children('section').hide();
// Find the href attribute value to identify the active tab + content
var activeTab = $(this).find('a').attr('href');
$(activeTab).fadeIn('fast'); // Fade in the active ID content
return false;
});
Here's the HTML:
<div class="tab-block grad-tabs">
<ul class="tab-list">
<li><a href="#tab1" title="tab 1">Tab 1</a></li>
<li><a href="#tab2" title="tab 2">Tab 2</a></li>
<li><a href="tab3" title="tab 3">Tab 3</a></li>
</ul>
<div class="tab-sections">
<section id="tab1">
<h2>Tab 1</h2>
<p>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</section>
<section id="tab2">
<h2>Tab 2</h2>
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
</section>
<section id="tab3">
<h2>Tab 3</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</section>
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以进行一些改进,对于您的主要问题,您可以使用
.find()
来解决.children()
问题其中:使用
.closest() 转到
然后.tab-block
.find()
你想要里面,像这样:对于其余的,是的,还有更多的优化,不需要最初的
。 each()
循环,因为它们都重复执行相同的操作,您可以进一步优化.click()
处理程序,如下所示:如果您使用的是 jQuery 1.4.1+,您甚至可以使用
.delegate()
,将其更改为:为此:
附加一个 <每个
.tab-list
元素都有一个 code>click 处理程序,而不是每个一个。
You can make a few more improvements, for your main question, you can use
.find()
to solve the.children()
problem, instead of this:Go up to the
.tab-block
using.closest()
then.find()
what you want inside, like this:For the rest, yes there are more optimizations, there's no need for the initial
.each()
loops since they all do the same thing repeatedly and you can further optimize your initial hiding and chaining inside the.click()
handler, like this:If you're on jQuery 1.4.1+ you can even further optimize this with
.delegate()
, changing this:To this:
This attaches one
click
handler per.tab-list
element, rather than one per<li>
.您可能对此感兴趣,
这里我缓存了这些部分,因为它在页面操作期间被多次使用。
再次使用 jquery delegate 方法,而不是向 3 个
li 中的每一个添加 1 个点击事件
元素。您可以在此处找到一个工作示例,但我认为您可以看看这个也是。
You may interested in this
Here I've cached the sections since it is used multiple times during the page actions.
Again the jquery delegate method is used instead of adding 1 click event to each of the 3
li
elements.You can find a working sample here, but I think you can have a look at this also.