帮助优化一些 jQuery 功能

发布于 2024-10-09 20:11:50 字数 2246 浏览 2 评论 0原文

我有一些简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怂人 2024-10-16 20:11:50

您还可以进行一些改进,对于您的主要问题,您可以使用 .find() 来解决 .children() 问题其中:

$(this).parents('.tab-block').children('.tab-sections').children('section').hide();

使用 .closest() 转到 .tab-block然后.find()你想要里面,像这样:

$(this).closest('.tab-block').find('.tab-sections section').hide();

对于其余的,是的,还有更多的优化,不需要最初的 。 each() 循环,因为它们都重复执行相同的操作,您可以进一步优化 .click() 处理程序,如下所示:

$('.tab-sections section:not(:first-child)').hide(); // Hide all content that not the first
$('.tab-list li:first-child').addClass('active');
$('.tab-list li').click(function() {
   $(this).addClass('active').siblings().removeClass('active'); // Add "active" class to selected tab, remove any other "active" class
   $(this).closest('.tab-block').find('.tab-sections section').hide(); // Hide tabs.
   var activeTab = $(this).find('a').attr('href'); // Find the href attribute value to identify the active tab + content
   $(activeTab).fadeIn('fast');                    // Fade in the active ID content
   return false;
});

如果您使用的是 jQuery 1.4.1+,您甚至可以使用 .delegate(),将其更改为:

$('.tab-list li').click(function() {

为此:

$('.tab-list').delegate('li', 'click', function() {

附加一个 <每个 .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:

    $(this).parents('.tab-block').children('.tab-sections').children('section').hide();
    

    Go up to the .tab-block using .closest() then .find() what you want inside, like this:

    $(this).closest('.tab-block').find('.tab-sections section').hide();
    

    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:

    $('.tab-sections section:not(:first-child)').hide(); // Hide all content that not the first
    $('.tab-list li:first-child').addClass('active');
    $('.tab-list li').click(function() {
       $(this).addClass('active').siblings().removeClass('active'); // Add "active" class to selected tab, remove any other "active" class
       $(this).closest('.tab-block').find('.tab-sections section').hide(); // Hide tabs.
       var activeTab = $(this).find('a').attr('href'); // Find the href attribute value to identify the active tab + content
       $(activeTab).fadeIn('fast');                    // Fade in the active ID content
       return false;
    });
    

    If you're on jQuery 1.4.1+ you can even further optimize this with .delegate(), changing this:

    $('.tab-list li').click(function() {
    

    To this:

    $('.tab-list').delegate('li', 'click', function() {
    

    This attaches one click handler per .tab-list element, rather than one per <li>.

    笑梦风尘 2024-10-16 20:11:50

    您可能对此感兴趣,

    var sections = $('.tab-sections section');
    
    sections.hide();
    
    $('.tab-list li:first-child').addClass('active');
    
    $('.tab-sections section:first-child').show();
    
    $('.tab-list').delegate("li", "click", function() {
    
        $(this).addClass('active').siblings().removeClass('active');
    
        sections.hide(); 
    
        var activeTab = $(this).children('a').attr('href'); 
        $(activeTab).fadeIn('fast');
    
    });
    

    这里我缓存了这些部分,因为它在页面操作期间被多次使用。

    再次使用 jquery delegate 方法,而不是向 3 个 li 中的每一个添加 1 个点击事件 元素。

    您可以在此处找到一个工作示例,但我认为您可以看看这个也是。

    You may interested in this

    var sections = $('.tab-sections section');
    
    sections.hide();
    
    $('.tab-list li:first-child').addClass('active');
    
    $('.tab-sections section:first-child').show();
    
    $('.tab-list').delegate("li", "click", function() {
    
        $(this).addClass('active').siblings().removeClass('active');
    
        sections.hide(); 
    
        var activeTab = $(this).children('a').attr('href'); 
        $(activeTab).fadeIn('fast');
    
    });
    

    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.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文