简体   繁体   English

有人可以解释这段代码在做什么吗?

[英]Can someone explain what this snippet of code is doing?

Im trying to learn javascript by tracing through some code at the moment, I was wondering if someone could explain what is happening in this snippet of code (this snippet is just part of a function hence no closing brace): 我试图通过查找当前代码来学习javascript,我想知道是否有人可以解释此代码段中发生的事情(此代码段只是函数的一部分,因此没有右括号):

document.addEventListener("keydown",function(e){

        for(i=0;i < keys.length; i++) {

            if(e.keyCode == keys[i]){

            var tri = document.getElementById("foo").childNodes[i];

            if(i==0){
                var tri = document.getElementById("foo").childNodes[1];
            }

            if(i==1){
                var tri = document.getElementById("foo").childNodes[3];
            }

            if(i > 1) {
                var tri = document.getElementById("foo").childNodes[(i*2)+1];
            }

The part im confused about most in this is the childNodes[] and the if(i) statements? 我最困惑的部分是childNodes []和if(i)语句?

// Bind an event handler to keydown on the entire document
document.addEventListener("keydown",function(e){

    // Everything in here happens on keydown

    // keys must be an array declared somewhere earlier in the code
    // This loops through that array
    for(i=0;i < keys.length; i++) {

        // If the current key we are looking at in the array
        // is the key that was pressed
        if(e.keyCode == keys[i]){

            // Get the (i+1)th childnode of foo
            var tri = document.getElementById("foo").childNodes[i];

            // If i = 0 get the second element (not the first)
            if(i==0){
                var tri = document.getElementById("foo").childNodes[1];
            }

            // If i == 1 get the fourth element (not the second)
            if(i==1){
               var tri = document.getElementById("foo").childNodes[3];
            }

            // Otherwise get the (i*2+2)th element.
            if(i > 1) {
                var tri = document.getElementById("foo").childNodes[(i*2)+1];
            }

            // Here we are still in an if-statement, in a loop, in a function, 
            // so there is probably more code here, at least some closing }'s

Note that var tri = document.getElementById("foo").childNodes[i]; 请注意, var tri = document.getElementById("foo").childNodes[i]; is a useless line, because i cannot be negative, one of the next three if statements will always succeed and tri will be overwritten. 是一条无用的行,因为i不能为负,所以接下来的三个if语句之一将始终成功并且tri将被覆盖。

Also note that when i = 0 , (i*2)+1 = 1 and when i = 1 , (i*2)+1 = 3 , so those other two if statements are useless as well because the third covers all cases and doesn't need to even be in an if clause. 另请注意,当i = 0(i*2)+1 = 1而当i = 1(i*2)+1 = 3 ,因此其他两个if语句也无用,因为第三个覆盖所有情况,并且甚至不需要在if子句中。 The above code is 100% equivalent to: 上面的代码100%等效于:

document.addEventListener("keydown",function(e){

    for(i=0;i < keys.length; i++) {

        if(e.keyCode == keys[i]){

            var tri = document.getElementById("foo").childNodes[(i*2)+1];

            ...

Since i is the variable used to iterate through the array called keys , and the node selected depends on i . 由于i是用于遍历称为keys的数组的变量,因此选择的节点取决于i keys must be an array with an unusual purpose. keys必须是具有非正常用途的数组。 It is an array of keyCodes, where the position of the keyCode in the array determines which node should be selected and stored in tri when that key is pressed. 它是一个keyCodes数组,其中keyCode在数组中的位置决定了按下该键时应选择哪个节点并将其存储在tri

Childnodes is the collection (array, effectively) of descendents of a DOM element. Childnodes是DOM元素后代的集合(有效地是数组)。

Eg consider some markup: 例如考虑一些标记:

<div id='div1'>
    <p id='p1'>
        <span id='s1'>Span one</span>
        <span id='s2'>Span two</span>
        <span id='s3'>Span three</span>
    </p>
</div>

in this scenario, document.getElementById('p1').childnodes[0] will return span with id='s1', childnodes[1] will return the span with id='s2' and so on. 在这种情况下,document.getElementById('p1')。childnodes [0]将返回id ='s1'的跨度,childnodes [1]将返回id ='s2'的跨度,依此类推。

http://www.w3schools.com/Dom/prop_element_childnodes.asp http://www.w3schools.com/Dom/prop_element_childnodes.asp

More detail: https://developer.mozilla.org/En/DOM/Node.childNodes 更多详细信息: https : //developer.mozilla.org/En/DOM/Node.childNodes

People will complain about a link to w3schools.com, but imho it is adequate for quick intros to concepts. 人们会抱怨到w3schools.com的链接,但是恕我直言,它足以快速介绍概念。

根据所按下的键,它会获得不同的DOM元素。

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

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