【问题标题】:Eliminating redundant code in a jQuery script消除 jQuery 脚本中的冗余代码
【发布时间】:2010-10-22 16:34:12
【问题描述】:

我已经设置了一个页面来通过 AJAX 加载数据,使用 jQuery .load 函数。

通过单击选项卡栏上的链接加载每个新文件时,我使用 jQuery 将选定选项卡的颜色设置为黄色。我尝试使用.toggleClass 函数将li 元素的类设置为活动的,这样它就会是黄色的,但没有骰子,所以我每次都求助于重置CSS。

如何消除冗余代码,或者对脚本进行大修?

无论如何,这里是 jQuery 脚本。欢迎任何帮助!

$(document).ready(function () {
    $('a#catalog').click(function() {
        $("#nav ul li a").css("color","white");
        $(this).css("color","yellow");
        $("#content").load("files/catalog.html");   
    });
    $('a#request').click(function() {
        $("#nav ul li a").css("color","white");
        $(this).css("color","yellow");      
        $("#content").load("files/request.html");
    });
    $('a#publisher').click(function() {
        $("#nav ul li a").css("color","white");
        $(this).css("color","yellow");
        $("#content").load("files/publisher.html");
      });
    $('a#incoming').click(function() {
        $("#nav ul li a").css("color","white");
        $(this).css("color","yellow");
        $("#content").load("files/incoming.html");
    });
    $('a#finished').click(function() {
        $("#nav ul li a").css("color","white");
        $(this).css("color","yellow");
        $("#content").load("files/finished.html");
    });
    $('a#shipments').click(function() {
        $("#nav ul li a").css("color","white");
        $(this).css("color","yellow");
        $("#content").load("files/shipments.html");
    });
});

还有导航栏:

<div class="bar" id="nav">
            <ul class="left">
                <li><a href="#" id="request">Request Search</a></li>
                <li><a href="#" id="catalog">Catalog Search</a></li>        
                <li><a href="#" id="publisher">Request from Publisher</a></li>
                <li><a href="#" id="incoming">Catalog Incoming Files</a></li>
                <li><a href="#" id="finished">Send Finished Files</a></li>      
                <li><a href="#" id="shipments">Shipments</a></li>
            </ul>
        </div>

最后但并非最不重要的一点是 CSS:

.bar { margin: 5px 0; position: relative; height: 20px; background-color: #515e6c; }
.bar a { color: #fff; }
.bar ul li a:hover { color: yellow; }
/* .bar ul li.active { color: yellow; } */
ul { margin: .3em 0; }
ul li { display: inline; padding: 0 5px; border-right: 1px solid #fff; }
ul li:last-child { border-right: none; }

提前致谢!

【问题讨论】:

    标签: javascript jquery css ajax


    【解决方案1】:

    应该这样做:

    $(document).ready(function () {
        var $links = $('#nav ul li a');
        $links.click(function() {
            $links.css("color","white");
            $(this).css("color","yellow");
            $("#content").load("files/" + $(this).attr('id') + ".html");       
        });
    });
    

    就您选择的规则而言,将其更改为:

    .bar ul li a.active { color: yellow; }
    

    然后你可以把click函数的前两行改成这样:

    $links.removeClass('active');
    $(this).addClass('active');
    

    样式最初应用于&lt;li&gt; 本身,而不是&lt;a&gt;。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      • 2010-11-13
      • 2010-11-09
      • 2011-03-13
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多