简体   繁体   English

澄清在jQuery中使用CSS

[英]clarification using css in jquery

Code below works which came from a tutorial ive been following. 下面的代码来自于我的教程。 i just want to understand some parts of the code. 我只想了解代码的某些部分。

function Arrow_Points() {
    var s = $('#container').find('.item');
    $.each(s, function (i, obj) {
        var posLeft = $(obj).css("left");

        if (posLeft == "0px") {
            html = "<span class='rightCorner'></span>";
            $(obj).prepend(html);
        } else {
            html = "<span class='leftCorner'></span>";
            $(obj).prepend(html);
        }
    });
}

1) what does the i in function used for? 1) i in函数的作用是什么? $.each(s,function(i,obj){ it was never used after it was declared. $.each(s,function(i,obj){这是从来没有宣布后使用。

2) this is my css for .item 2)这是我的.item CSS

.item {
width: 408px;
float: left;
min-height:50px;
}

the condition is if(posLeft == "0px") how did he/she came up with the value 0px ? 条件是if(posLeft == "0px")他/她是如何得出0px值的? are left floats default position is 0px ? left浮动默认位置是0px吗?

the i in the $.each iteration refers to the index of the element you are iterating over. $.each迭代中的i $.each迭代的元素的索引。

Specifically, for any Enumerable s , at every iteration of $.each() , s[i]===obj 具体来说,对于任何Enumerable s ,在$.each()每次迭代中, s[i]===obj

See the official documentation of $.each 请参阅$ .each的官方文档

CSS: CSS:

float makes the element stick to left side of its parent container element. float使元素固定在其父容器元素的左侧。

So if the element is nested in the body element it should be predictably be on the left corner BUT css left must be explicitly defined in order to return a value . 因此,如果该元素嵌套在body元素中,则应该可以预测它在左上角, 但是必须显式定义left css,以便返回一个值。

How ever I see no relation between .item and this if condition. 我怎么也看不到.item和这个if条件之间的关系。

Each: 每:

As for for the i it used for the index, when it's not used it is sometimes marked as _ . 至于它用于索引的i ,当不使用它时,有时会标记为_ You need to declare it if you want to handle each object as obj since it is the second argument this function takes and javscript "can't" guess what you meant when you wrote obj without it's counterpart. 如果要将每个对象都作为obj处理,则需要声明它,因为它是该函数接受的第二个参数,并且javscript“无法”猜测在没有obj情况下编写obj时的意思。 obj however is optional so if you only pass index it will work. obj是可选的,因此,如果仅传递index ,它将起作用。

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

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