English 中文(简体)
难道有什么更好的办法来撰写我的《 j法》?
原标题:Is there any better way to write my jQuery Code?

我写了 some的一些文字,显示并暗中隐藏在工作人员网页上的信息。 基本上有3个标志和3个描述。 如果你在设计标志上 h,就会显示设计小组的描述和形象,反之亦然。 我的守则就是这样做的:

$( .emblem img:first ).hover(
            function() {
                $( .description:eq(1), .description:eq(2) ).css({opacity : 0.2});
                $( .devStaff ).css({opacity : 0.2});
            },
            function(){
                $( .description:eq(1), .description:eq(2) ).css({opacity : 1});
                $( .devStaff ).css({opacity : 1});
            }
        );

        $( .emblem img:eq(1) ).hover(
            function() {
                $( .description:eq(0), .description:eq(2) ).css({opacity : 0.2});
                $( .designStaff ).css({opacity : 0.2});
            },
            function(){
                $( .description:eq(0), .description:eq(2) ).css({opacity : 1});
                $( .designStaff ).css({opacity : 1});
            }
        );

        $( .emblem img:eq(2) ).hover(
            function() {
                $( .description:eq(0), .description:eq(1) ).css({opacity : 0.2});
                $( .designStaff ).css({opacity : 0.2});
            },
            function(){
                $( .description:eq(0), .description:eq(1) ).css({opacity : 1});
                $( .designStaff ).css({opacity : 1});
            }
        );

现在,我想到这一点,我感觉到,这样做肯定有更好的办法,我不知道任何人是否能够提供一些建议?

最佳回答

总的来说,你应重复(http://en.wikipedia.org/wiki/Don t_repeat_your Self),

要求发挥更一般性的职能,如:

function fadeOut(eqNum1, eqNum2) {
    $( .description:eq( +eqNum1+ ), .description:eq( +eqNum2+ ) ).css({opacity : 0.2});
    $( .devStaff ).css({opacity : 0.2});
}
 function fadeIn(eqNum1, eqNum2){
    $( .description:eq( +eqNum1+ ), .description:eq( +eqNum2+ ) ).css({opacity : 1});
    $( .devStaff ).css({opacity : 1});
}

$( .emblem img:first ).hover(fadeOut(1,2), fadeIn(1,2) );

$( .emblem img:eq(1) ).hover(fadeOut(0,2),fadeIn(0,2));

$( .emblem img:eq(2) ).hover(fadeOut(0,1),fadeIn(0,1));
问题回答

You can replace :first, :eq(1) and :eq(2) by :lt(3) :

    $( .emblem img:lt(3) ).hover(
        function() {
            $( .description:lt(2) ).css({opacity : 0.2});
            $( .designStaff ).css({opacity : 0.2});
        },
        function(){
            $( .description:lt(2) ).css({opacity : 1});
            $( .designStaff ).css({opacity : 1});
        }
    );

我遵循我刚才所写的这些规则,至少要看一看一看,用一比一等,作为你起码的工作。

http://www.artzstudio.com/2009/04/jquery- Performance-rules/





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签