简体   繁体   English

有人可以帮我解释一下此代码吗?

[英]Can someone explain this code for me?

Hi im using this code for a list of links that have an animation on their padding. 大家好,我使用此代码获取在其填充上具有动画的链接列表。 I wanted when i clicked on it to keep that padding and not animate back to its original position. 当我单击它时,我想要保留该填充,而不是重新设置其原始动画。 What i dont understand are two things. 我不明白的是两件事。 " if (!$(this) " that part of code and how when i dont have a class of current on any link does it find any? Thanks so much! if (!$(this) ”那部分代码以及当我在任何链接上都没有电流类时如何找到它?非常感谢!

$(function() {
    $('#secondary_content_what_we_do li a').hover(function() {
        if (!$(this).hasClass("current")) {
            $(this).stop().animate({
                "paddingLeft": "10px"
            }, 400).addClass('column_hover');
        }
    }, function() {
        if (!$(this).hasClass("current")) {
            $(this).stop().animate({
                "paddingLeft": "0px"
            }, 'slow').removeClass('column_hover');
        }
    });

    $('#secondary_content_what_we_do a').click(function() {
        $('a').removeClass('column_active').removeClass("current");
        $(this).addClass('column_active').addClass("current");
        $('#secondary_content_what_we_do li a').trigger('mouseleave');
        var url = $(this).attr('href');
        $('#loading_content').hide().load(url).fadeIn(1000);
        return false;
    });
})

UPDATE For some reason i cant comment. 由于某种原因,我无法发表评论。 I understand now, but i dont have a class current in my css so i dont know how that line helps. 我现在知道了,但是我的CSS中没有当前的课程,所以我不知道这条线有什么帮助。

The if (!$(this) is not a complete expression; the ! is negating, for example, $(this).hasClass("current") . It would work the same, and maybe be clearer to you, if you wrote !($(this).hasClass("current")) . That expression means "if this object does not have the class 'current'". if (!$(this)不是一个完整的表达式; !表示否定,例如$(this).hasClass("current") ,它的工作原理相同,如果您写了,可能会更清楚!($(this).hasClass("current")) 。该表达式的意思是“如果此对象没有类'current'”。

The class "current" is getting added to things by the line 该行将“ current”类添加到事物中

$(this).addClass('column_active').addClass("current");

This statement: 这个说法:

if (!$(this).hasClass("current"))

Is doing several things. 正在做几件事。 It's wrapping the this variable (which in this case refers to the element that was hovered over) in a jQuery object (using $(...) ), and then calling the hasClass(...) method on it, and then notting ( ! ) the result. 它将this变量(在本例中是指被悬停的元素)包装在jQuery对象中(使用$(...) ),然后在其上调用hasClass(...)方法,然后注意( ! )结果。 The statement reads " If the element that was hovered over does not have the class "current" then execute the code within the block " 语句显示为“ 如果悬停的元素不具有“当前”类,则在块内执行代码

As for the second part of your question, I think you're referring to this line: 至于问题的第二部分,我认为您指的是这一行:

$('a').removeClass('column_active').removeClass("current");

And how it works if the anchor selected doesn't have a class of current . 以及如果选定的锚没有一类current话它是如何工作的。 If the a element selected has no current class, the statement will have no effect (Please let me know if this isn't what you're asking). 如果a选择的元素没有current类,声明不会有任何影响(请让我知道这是不是你在问什么)。

$('#secondary_content_what_we_do li a')

selects ALL a elements which are descendants of an li which is itself a descendent of the element with id secondary_content_what_we_do . 选择所有a li后代的元素,而li本身是id为secondary_content_what_we_do的元素的后代。 So this selector returns an array of jQuery wrapped DOM elements. 因此,此选择器返回jQuery包装的DOM元素数组

The this keyword inside the function refers to the single DOM element which triggered the event ( click , hover or whatever). 函数内的this关键字指的是触发事件的单个 DOM元素( clickhover或其他)。

$(this) simply wraps the native DOM this element as a jQuery object again so we can access the jQuery functions like addClass and removeClass $(this)只是将原生DOM this元素再次包装为jQuery对象,因此我们可以访问jQuery函数,例如addClassremoveClass

so... 所以...

$('#secondary_content_what_we_do li a').hover(function() {
        if (!$(this).hasClass("current")) {
            doSomething();
        }
}

essentialy is saying when we hover over any element matched by the selector, if the specific element we hovered over doesn't have the class current (the ! operator reverses the result of the hasClass call), then do something. 本质上是说,当我们将鼠标悬停在与选择器匹配的任何元素上时,如果我们悬停的特定元素没有 current的类( !运算符会反转hasClass调用的结果),然后执行一些操作。

HTML DOM elements can have more than one class at the same time. HTML DOM元素可以同时具有多个类。 Does that clear thing up? 这样清除了吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM