The following code implements the function of the tab, where a [I] .index = I; and p [this.index] do not understand.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
    <style>
        a{
            text-decoration: none;
            display: inline-block;
            background: -sharp000;
            color: -sharpfff;
            text-align: center;
            width: 100px;
            height:40px;
            line-height: 40px;
            font-size: 25px;
        }
        p{
            border:4px solid -sharp000;
            display: none;
            height: 300px;
        }
    </style>
</head>
<body>
<a style="background: red;" href="-sharp"></a>
<a href="-sharp"></a>
<a href="-sharp"></a>
<p style="display: block">

<script> var a = document.querySelectorAll("a"); var p = document.querySelectorAll("p"); for(var i=0;i<a.length;iPP){ a[i].index = i; a[i].onclick = function () { for(var j=0;j<a.length;jPP){ a[j].style.background = "-sharp000"; p[j].style.display = "none"; } this.style.background = "red"; p[this.index].style.display = "block"; } } </script> </body> </html>

where a [I] .index = I in js code and p [this.index] .style.display = "block"; do not understand?
Why can"t you directly use p [I] .style.display ?
the line a [I] .index = I; what does the code mean

Mar.07,2021

because the function executed by click is equivalent to asynchronous execution, while I always keeps the global reference , when you trigger the click event, I is actually a.length . So you need to use a [I] .index = I to save the index value of each dom node. Of course, you can also use closures. The code is as follows:

  

this is an action assigned to multiple a tags. Because there are multiple a tags in the previous query, a [I] takes different a tag elements in turn for processing. The
p tag is similar.
this should be a toggle tab effect, which hides all p tags first, and then selects an unhide to display. p [this.index] here, since it refers to the trigger function of a, the index value of the corresponding a will be taken.

Menu