繁体   English   中英

使用 for 循环迭代项目以更改弹出窗口的文本

[英]Iterate over items using for loop to change text of popup

客观的

  • 每次点击.player都会出现一个弹出窗口,使用.html()将其更改为来自players.js有关该玩家的相应数据
  • 问题:我正在使用 for 循环遍历所有玩家,但是现在当我点击一个玩家时,它似乎运行了 50 次,而不是一次。 所以当我console.log(players.length)我让玩家在第 50 个位置,而不是那个玩家。
  • 因此,当我单击任何.player我想获取该玩家的数据

脚本.js

function getData() {
    $(".player").click(function(){
        for (var i = 0; i < players.length; i++) {
            $(".popup__position").html(players[i].posfixed);
            $(".popup__name").html(players[i].name);
            $(".popup__years").html(players[i].year);
            $(".popup__description").html(players[i].highlights);
            $(".popup__stats").html(players[i].notablestats);
        }
    });
}

index.html 的片段(有 50 个播放器,它们都采用相同的格式)

<div class="popup clearfix">
    <div class="icon-container">
        <i class="fa fa-times" aria-hidden="true"></i>
    </div>
    <img src="" alt="" class="popup__picture">

    <div class="popup__text">
        <p class="popup__position">tk-position</p>
        <p class="popup__name">tk-name</p>
        <p class="popup__years">tk-year</p>
        <p class="popup__description">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sequi ad dicta sunt unde, sed quae nihil inventore voluptates nulla voluptate laudantium nesciunt quo, aspernatur deleniti quod harum, nisi error doloribus.</p>
        <div class="popup__stats">
            <p>tk-stats</p>
        </div>
        <button class="btn--add">Add to team</button>
        <button class="btn--reset">Reset picks</button>
    </div>
</div>


<div class="player player--bobplager player--defenseman">
    <div class="player__info animated">
        <p class="player__name">Bob Plager</p>
        <p class="player__position">Defenseman</p>
    </div>
</div>

<div class="player player--shanahan player--forward">
    <div class="player__info animated">
        <p class="player__name">Brendan Shanahan</p>
        <p class="player__position">Forward</p>
    </div>
</div>

<div class="player player--hull player--forward">
    <div class="player__info animated">
        <p class="player__name">Brett Hull</p>
        <p class="player__position ">Forward</p>
    </div>
</div>

<div class="player player--elliott player--goalie">
    <div class="player__info animated">
        <p class="player__name">Brian Elliott</p>
        <p class="player__position ">Goalie</p>
    </div>
</div>

玩家.js

var players = [
    {
        "name": "Adam Oates",
        "highlights": "Oates was traded to the Blues for a fan favorite in Bernie Federko, spent just 2 1/2 seasons with the club and left after asking to renegotiate his contract again. But despite those factors, he is revered as the best set-up man to ever play for the Blues, one-half of the dynamic \"Hull & Oates\" duo.",
        "posfixed": "Forward",
        "year": "1989-92",
        "notablestats": "195 games, 58 goals, 286 points"
    },
    {
        "name": "Al Arbour",
        "highlights": "Arbour was selected in the NHL expansion draft for a specific reason: to be the Blues' original captain. But the 35-year-old was more than a leader, earning back-to-back All-Star appearances. The last NHL player to wear framed glasses on the ice, Arbour retired and began his legendary coaching career with the Blues.",
        "posfixed": "Defenseman",
        "year": "1967-1971",
        "notablestats": "231 games, 2 goals, 23 points, plus-27"
    },
    {
        "name": "Al MacInnis",
        "highlights": "MacInnis was known for his lethal blast from the blueline, winning the NHL's hardest-shot competition seven times between 1991 and 2003. It helped him rack up 325 assists with the Blues, which ranks fourth among all players in team history and No. 1 among defensemen.",
        "posfixed": "Defenseman",
        "year": "1994-2005",
        "notablestats": "613 games, 127 goals, 452 points, plus-132"
    },
    {
        "name": "Alex Pietrangelo",
        "highlights": "Pietrangelo was the No. 4 overall pick in 2008, but in back-to-back years, the Blues deemed him unready for the NHL and sent him back to junior hockey. The wait was worth it, though, as Pietrangelo has become a reliable No. 1 defenseman who is among the league leaders in ice time.",
        "posfixed": "Defenseman",
        "year": "2008-present",
        "notablestats": "459 games, 51 goals, 255 points, plus-53"
    },
    {
        "name": "Alexander Steen",
        "posfixed": "Forward",
        "year": "2008-present",
        "notablestats": "493 games, 147 goals, 355 points"
    },
    {
        "name": "Barclay Plager",
        "highlights": "Plager was the heart and soul of the organization for many years before his untimely death from cancer in 1988. He was hand-picked by Scotty Bowman to become a Blue in a trade and became one of team's most valuable defenseman. He also served as captain and later as head coach.",
        "posfixed": "Defense",
        "year": "1967-77",
        "notablestats": "614 games, 44 goals, 231 points, plus-58"
    },
    {
        "name": "Barret Jackman",
        "highlights": "Jackman broke into the NHL by winning the Calder Trophy in 2003 and he lasted 13 seasons with the Blues by adapting his physical style of play to the league's ever-changing rules. He ranks No. 2 in franchise history in games played with 803 and is the only defenseman in the top six on that list.",
        "posfixed": "Defenseman",
        "year": "2002-2015",
        "notablestats": "803 games, 28 goals, 181 points, plus-53"
    },
    {
        "name": "Bernie Federko",
        "highlights": "No player has ever dressed for more games (927), or netted more points (1,073), in a Blues' uniform than Federko. One of the elite centers of his time, he became the first player who spent the bulk of his career with the club to be inducted into the Hockey Hall of Fame in 2002.",
        "posfixed": "Forward",
        "year": "1976-89",
        "notablestats": "927 games, 352 goals, 1,073 points"
    },
    {
        "name": "Blake Dunlop",
        "highlights": "The undersized Dunlop (5-foot-10, 170 pounds) was assigned to the minor leagues six times and traded twice when he became a Blue in 1979. He persevered, however, becoming one of the Blues' biggest offensive contributors with 87 points in 1980-81 and a reliable two-way center.",
        "posfixed": "Forward",
        "year": "1979-83",
        "notablestats": "324 games, 86 goals, 287 points"
    },
    {
        "name": "Bob Gassoff",
        "highlights": "At 5-foot-10 and 190 pounds, Gassoff is considered the best pound-for-pound competitor to ever play for the club. He dropped the gloves 36 times and had 866 penalty minutes in just 245 games before his career was tragically cut short by a motor-cycle accident in 1977.",
        "posfixed": "Defenseman",
        "year": "1973-77",
        "notablestats": "245 games, 11 goals, 58 points, minus-15, 866 penalty minutes"
    },
    {
        "name": "Bob Plager",
        "highlights": "A member of the original Blues' roster, Plager became popular because of his patented hipcheck. He has held every title imaginable in the organization in the past 50 years and today remains the team's No. 1 ambassador. In a franchise full of colorful figures, he is the most colorful.",
        "posfixed": "Defenseman",
        "year": "1967-78",
        "notablestats": "615 games, 20 goals, 141 points, plus-27"
    },
    {
        "name": "Brendan Shanahan",
        "highlights": "Shanahan is one of only three players in Blues' history to net 50-plus goals in a single season and one of two to accomplish it twice (51 goals in 1992-93, 52 in '93-94). His skill level coupled with his toughness (692 penalty minutes in 3 1/2 seasons) made him a cult figure with the fans.",
        "posfixed": "Forward",
        "year": "1991-95",
        "notablestats": "277 games, 156 goals, 306 points"
    },
    {
        "name": "Brett Hull",
        "highlights": "After arriving from Calgary in arguably the best trade in club history, Hull revitalized hockey in St. Louis in the 1980s and '90s. He left as the franchise leader in goals (527), hat tricks (27), game-winning goals (70), power-play goals (195) and shots on goal (3,367).",
        "posfixed": "Forward",
        "year": "1988-98",
        "notablestats": "744 games, 527 goals, 936 points"
    },
    {
        "name": "Brian Elliott",
        "highlights": "Elliott signed a one-year contract with the Blues in 2011 and left five years later as the franchise's all-time leader in goals-against average (2.01), save percentage (.925) and shutouts (25). He also took the team to its first trip to the Western Conference finals in 15 years.",
        "posfixed": "Goalie",
        "year": "2011-16",
        "notablestats": "180 games, 164-104-46, 2.01 GAA, .925 save-percentage, 25 shutouts"
    },
    {
        "name": "Brian Sutter",
        "highlights": "From a four-year reign as head coach to a nine-year run as captain, Sutter epitomized the definition of being a Blue. He ranks third in games played (779), goals (303), assists (333) and points (636), all while racking up a franchise-leading 1,786 penalty minutes.",
        "posfixed": "Forward",
        "year": "1976-1988",
        "notablestats": "779 games, 303 goals, 636 points"
    },
    {
        "name": "Chris Pronger",
        "highlights": "Pronger came to town under unfair conditions, acquired in Mike Keenan's controversial trade for Brendan Shanahan. But he won over the hearts of Blues' fans by winning the Hart and Norris Trophy in 2000 and developing into one of the nastiest defenseman in NHL history.",
        "posfixed": "Defenseman",
        "year": "1995-2005",
        "notablestats": "598 games, 84 goals, 356 points, plus-140"
    },
    {
        "name": "Chuck Lefley",
        "highlights": "Lefley was one of the flashiest skaters to ever wear the Blue Note, his speed enough to bring fans out of their seats when he had open ice. The left winger's 43 goals in 1975-76 broke Gary Unger's team record of 41 and stood as the top mark until Wayne Babych shattered it with 54 in '80-81.",
        "posfixed": "Forward",
        "year": "1974-81",
        "notablestats": "5 seasons, 233 games, 83 goals, 187 points"
    },
    {
        "name": "Craig Janney",
        "highlights": "Janney was traded to the Blues for Adam Oates, whose contract dispute put a premature end to his brilliant run with Brett Hull. That put more pressure on Janney, but his 106-point season in 1992-93, making him one of only six players in club history to eclipse 100 points, made the deal easier to swallow.",
        "posfixed": "Forward",
        "year": "1992-94, 1994-95",
        "notablestats": "4 seasons, 186 games, 48 goals, 233 points"
    },
    {
        "name": "Curtis Joseph",
        "highlights": "Joseph, who went by the nickname 'Cujo,' was arguably the most athletic and acrobatic goalie to play in the Blues' crease. The undrafted netminder set a franchise-record in 1993-94 with 36 wins, and later ascended to No. 2 all-time behind Mike Liut in both wins (137) and games played (280).",
        "posfixed": "Goalie",
        "year": "1989-95",
        "notablestats": "280 games, 137-96-34, 3.04 GAA, .907, 5 shutouts"
    },
    {
        "name": "David Backes",
        "highlights": "Backes grew up with the Blues through some lean seasons, and after being named captain in 2011, he led them to five straight playoff appearances. He left the organization ranked No. 5 in games played (727), No. 6 in goals (206), No. 8 in assists (254) and No. 6 in points (460).",
        "posfixed": "Forward",
        "year": "2006-16",
        "notablestats": "727 games, 206 goals, 460 points"
    },
    {
        "name": "Doug Gilmour",
        "highlights": "The Blues went small with the selection of Gilmour (5-foot-11, 177 pounds) in the seventh round of the 1982 NHL draft, but they struck gold. He put up 354 points in 384 games by the age of 24, when an off-ice incident forced a trade and ended here what became a Hockey Hall of Fame career.",
        "posfixed": "Forward",
        "year": "1983-88",
        "notablestats": "384 games, 149 goals, 354 points"
    },
    {
        "name": "Doug Harvey",
        "highlights": "Harvey joined the Blues' organization at the end of his legendary career with Montreal. He began as a player-coach with the minor-league club in Kansas City, but came to St. Louis and provided invaluable experience for the 1967-68 playoff run as well as the '68-69 season.",
        "posfixed": "Defenseman",
        "year": "1968-69",
        "notablestats": "70 games, 2 goals, 22 points, plus-11"
    },
    {
        "name": "Doug Weight",
        "highlights": "Weight's name doesn't appear often in the Blues' record books, but he was one of the most gifted natural centers in franchise history. A big pickup from Edmonton in 2001, he posted back-to-back seasons of 50-plus assists and five straight seasons of 30 or more helpers.",
        "posfixed": "Forward",
        "year": "2001-2006, '06-'07",
        "notablestats": "364 games, 75 goals, 295 points"
    },
    {
        "name": "Frank St. Marseille",
        "posfixed": "Forward",
        "year": "1967-73",
        "notablestats": "403 games, 86 goals, 257 points"
    },
    {
        "name": "Garry Unger",
        "highlights": "Replacing Red Berenson after the two were traded for one another in the early 1970s, Unger became the face of the franchise, recording eight straight 30-goal seasons. He was also known for his 'Ironman' streak of 914 consecutive games played, an NHL record that was later eclipsed.",
        "posfixed": "Forward",
        "year": "1971-79",
        "notablestats": "662 games, 292 goals, 575 points"
    },
    {
        "name": "Gary Sabourin",
        "posfixed": "Forward",
        "year": "1967-74",
        "notablestats": "463 games, 136 goals, 267 points,"
    },
    {
        "name": "Geoff Courtnall",
        "highlights": "Courtnall was one of the Blues' best spark plugs, no scene more memorable than his hit in the 1998 playoffs on Los Angeles goalie Jamie Storr. After the Kings retaliated, the Blues had a five-minute power play, on which they scored four goals and rallied for a 4-3 victory.",
        "posfixed": "Forward",
        "year": "1990-91, '95-99",
        "notablestats": "6 seasons, 326 games, 106 goals, 232 points"
    },
    {
        "name": "Glenn Hall",
        "highlights": "Hall's NHL career was coming to a close when the Blues begged him to play a few more seasons. They needed a goalie to put the franchise on the map and he obliged, taking them to three straight Stanley Cup finals. They were swept in each, but without the 1968 Conn Smythe winner, they would have been embarrassed.",
        "posfixed": "Goalie",
        "year": "1967-71",
        "notablestats": "140 games, 58-52-28, 2.43 GAA"
    },
    {
        "name": "Grant Fuhr",
        "highlights": "Fuhr had already made his mark in the NHL, winning five Stanley Cup with Edmonton, but he was far from done when he signed with the Blues as a free agent at age 33. In the 1995-96 season, he set the NHL record for consecutive games by a goalie (76) and overall appearances (79).",
        "posfixed": "Goalie",
        "year": "1995-99",
        "notablestats": "249 games, 108-87-41, 2.68 GAA, .908 save-percentage, 11 shutouts"
    },
    {
        "name": "Jacques Plante",
        "highlights": "Plante cemented his status in NHL history before joining the Blues in 1968, but had plenty more to give. After a three-year layoff, he returned to the league to create a terrific tandem with Glenn Hall. Plante had 13 shutouts in '68-69 and shared the Vezina Trophy with Hall, his seventh.",
        "posfixed": "Goalie",
        "year": "1968-70",
        "notablestats": "69 games, 36-21-11, 2.07 GAA, 10 shutouts"
    },
    {
        "name": "Jeff Brown",
        "highlights": "The Blues have had several offensive-minded defensemen in their 50-year history, but few as prolific as Brown. He holds the single-season record for goals (25) and points (78) by a defenseman, which he set in 1992-93. He also had 38 points in 42 playoff games.",
        "posfixed": "Defenseman",
        "year": "1989-94",
        "notablestats": "329 games, 80 goals, 294 points, minus-19"
    },
    {
        "name": "Jim Roberts",
        "highlights": "An \"Original Blue,\" Roberts was chosen by the club in the 1967 expansion draft and, taken third after two goaltenders, he is known as the first skater in the team's history. The sturdy defenseman also spent six seasons behind the Blues' bench as an assistant coach.",
        "posfixed": "Defenseman",
        "year": "1967-71",
        "notablestats": "6 seasons, 395 games, 63 goals, 157 points, plus-14"
    },
    {
        "name": "Joe Mullen",
        "highlights": "One of the best undrafted players in NHL history who became one of the most prolific American-born point-producers in the world, Mullen got his start as a free-agent signee with the Blues in 1979. He had back-to-back 40-goal seasons before the payroll-conscious club traded him to Calgary in 1986.",
        "posfixed": "Forward",
        "year": "1979-86",
        "notablestats": "301 games, 151 goals, 335 points"
    },
    {
        "name": "Jorgen Pettersson",
        "highlights": "Signed a free agent after five seasons in the Swedish Elite League, Pettersson burst onto the scene in 1980-81 and set the Blues' rookie record for most goals (37) and points (73). In each of his five seasons, the player who may be the club's best-ever European netted 20-plus goals and 30-plus assists.",
        "posfixed": "Forward",
        "year": "1980-85",
        "notablestats": "365 games, 161 goals, 332 points"
    },
    {
        "name": "Keith Tkachuk",
        "highlights": "One of the best power forwards of his generation, Tkachuk spent years parked in front of the opposition's net. His 208 goals rank No. 5 in team history, and his 96 power-play goals place him No. 4 all-time. He is also one of 43 players in NHL history to reach the 500-goal plateau.",
        "posfixed": "Forward",
        "year": "2001-07, '07-10",
        "notablestats": "543 games, 208 goals, 427 points"
    },
    {
        "name": "Kelly Chase",
        "highlights": "As far as ambassadors go, the popular Chase may be second to only Plager. He established himself on the ice as a legitimate lightweight enforcer, the perfect complement to Tony Twist's heavyweight hands. He ranks second in club history with 1,497 penalty minutes, trailing only Brian Sutter.",
        "posfixed": "Forward",
        "year": "1988-95, '97-2000",
        "notablestats": "345 games, 14 goals, 40 points, 1,497 penalty minutes"
    },
    {
        "name": "Larry Patey",
        "highlights": "Patey was part of a Blues' penalty-killing unit that did more than kill penalties, setting a team record with 21 shorthanded goals in 1975-76. He tied the single-season record with eight in 1980-81, and finished his Blues' career with 23 of his 123 goals coming shorthanded.",
        "posfixed": "Forward",
        "year": "1975-84",
        "notablestats": "603 games, 123 goals (23 shorthanded), 259 points"
    },
    {
        "name": "Mike Liut",
        "highlights": "Liut backstopped the Blues to their first 100-point season in 1980-81 with a record of 33-14-13, marking his second straight 30-win year. In just six seasons, he established club records in games played (347), minutes played (20,010) and wins (151) that still stand today.",
        "posfixed": "Goalie",
        "year": "1979-85",
        "notablestats": "347 games, 151-133-52, 3.58 GAA, .882 save-percentage, 10 shutouts"
    },
    {
        "name": "Pavol Demitra",
        "highlights": "The Blues acquired the little-known Demitra in a trade with Ottawa in 1996 and watched him blossom into a star. He led the club in scoring four times in a six-year span, climbing to No. 5 in club history in both assists (289) and points (493). He perished in a plane accident with his Russian hockey team in 2011.",
        "posfixed": "Forward",
        "year": "1996-2004",
        "notablestats": "494 games, 204 goals, 493 points"
    },
    {
        "name": "Perry Turnbull",
        "highlights": "The Blues had only four draft picks in 1979, but No. 2 overall was the highest in the team's history. They chose Turnbull, who was a quick study, scoring 34 goals in his second season for the first of three 30-goal seasons. He was dealt to Montreal for Doug Wickenheiser, but later had a second stint.",
        "posfixed": "Forward",
        "year": "1979-83, '87-88",
        "notablestats": "396 games, 139 goals, 238 points"
    },
    {
        "name": "Pierre Turgeon",
        "highlights": "Turgeon was a point-per-game playmaking center for the Blues, who helped anchor the middle during the Blues' run to the Presidents' Trophy in 1999-2000. He also scored one of the more meaningful goals in playoff history, the lone tally in a 1-0 overtime win over Phoenix in the 1999 conference quarterfinals.",
        "posfixed": "Forward",
        "year": "1996-2001",
        "notablestats": "327 games, 134 goals, 355 points"
    },
    {
        "name": "Red Berenson",
        "highlights": "Berenson is most remembered for his six-goal game on Nov. 7, 1968, a mark that remains a franchise record. But in two stints with the Blues, he piled up 412 points, which ranks No. 9 in team history. He also coached the club for three seasons, leading them to a 107-point season in 1980-81.",
        "posfixed": "Forward",
        "year": "1967-71",
        "notablestats": "519 games, 172 goals, 412 points"
    },
    {
        "name": "Rick Meagher",
        "posfixed": "Forward",
        "year": "1985-91",
        "notablestats": "413 games, 73 goals, 161 points"
    },
    {
        "name": "Rob Ramage",
        "highlights": "The Blues sent a first-round pick to New Jersey for Ramage, who was the No. 1 overall pick in 1979, but had fallen short of expectations. He returned to elite level with two 60-point seasons and a trip to the NHL all-star game before the Blues gave him up in the trade with Calgary for Brett Hull.",
        "posfixed": "Defenseman",
        "year": "1982-88",
        "notablestats": "441 games, 67 goals, 296 points, minus-41"
    },
    {
        "name": "Scott Stevens",
        "highlights": "Stevens signed a free-agent contract in 1990 that finally gave the Blues a franchise defenseman. Finishing seventh in Norris Trophy voting, he had a heavy impact on a club that finished with 105 points, but the fun was short-lived as an arbitrator sent Stevens to New Jersey as compensation for the team signing Brendan Shanahan.",
        "posfixed": "Defenseman",
        "year": "1990-91",
        "notablestats": "78 games, 5 goals, 49 points, plus-23"
    },
    {
        "name": "Scott Young",
        "posfixed": "Forward",
        "year": "1998-2002",
        "notablestats": "377 games, 125 goals, 254 points"
    },
    {
        "name": "Steve Duchesne",
        "posfixed": "Defenseman",
        "year": "1994-95, '97-98",
        "notablestats": "163 games, 38 goals, 125 points, plus-39"
    },
    {
        "name": "Tony Twist",
        "highlights": "One of the biggest bruisers in NHL history, Twist was as intimidating as anyone on skates. He never had more than eight points in season, but with 104 fights in 445 games, he affected the outcome more than the scoreboard indicated. A motor-cycle accident ended his career prematurely.",
        "posfixed": "Forward",
        "year": "1989-91, '94-99",
        "notablestats": "294 games, 10 goals, 21 points, 688 penalty minutes"
    },
    {
        "name": "Vladimir Tarasenko",
        "highlights": "Tarasenko was drafted No. 16 overall in 2010, and in short time he has emerged as the Blues' best goal-scoring threat since Brett Hull. In 2015-16, Tarasenko became the club's first 40-goal scorer since Brad Boyes in 2007-08, and he's added 19 goals in 33 postseason games.",
        "posfixed": "Forward",
        "year": "2012-present",
        "notablestats": "259 games, 106 goals, 209 points"
    },
    {
        "name": "Wayne Babych",
        "highlights": "Before there was Brett Hull and Brendan Shanahan, Babych was the first 50-goal scorer in Blues' history. In the club's 14th year of existence and his third season in the league, Babych netted 54 goals in 1980-81. It would be his high-water mark, as injuries derailed the remainder of his career here.",
        "posfixed": "Forward",
        "year": "1978-84",
        "notablestats": "396 games, 155 goals, 345 points"
    }
]

使用选择器 $(".popup__position") 会将它分配给 DOM 中与类“.popup_position”匹配的所有给定节点; 因此,当你循环它 50 次时,听众的堆栈总数为 50。

为什么不在 JSON 中包含一个 id,并将点击事件链接到每个玩家的顶级 div 中的一个 id。

带有 id 属性和需要 json 元素的可能示例。

function getData() {
    for (var i = 0; i < players.length; i++) {
    var currentPlayerIdSelector = "#"+players[i].id;
        $("#"+players[i].id).click(function(){
            $(currentPlayerIdSelector+" .popup__position").html(players[i].posfixed);
            $(currentPlayerIdSelector+" .popup__name").html(players[i].name);
            $(currentPlayerIdSelector+" .popup__years").html(players[i].year);
            $(currentPlayerIdSelector+" .popup__description").html(players[i].highlights);
            $(currentPlayerIdSelector+" .popup__stats").html(players[i].notablestats);
        }
    }
}

如果每个玩家名称都是唯一的,您可以使用它来过滤players数组。 像这样的东西。

$(".player").click(function(){
    var pName = $('.player__name',this).text().trim();
    var selPlayers = players.filter(function(player){
         return (player.name == pName);
       }); // single result expected

        $(".popup__position").html(selPlayers[0].posfixed);
        $(".popup__name").html(selPlayers[0].name);
        $(".popup__years").html(selPlayers[0].year);
        $(".popup__description").html(selPlayers[0].highlights);
        $(".popup__stats").html(selPlayers[0].notablestats);

});
  • 为每个玩家添加了一个data-id
  • 创建了一个名为playerID的变量并用它代替 for 循环从players.js获取相应的数据

脚本.js

function getData() {
    $(".player").click(function(){
        var playerID = $(this).attr("data-id") -1;
        console.log(playerID);

        var playerPos = $(".popup__position").html(players[playerID].position);
        var playerName = $(".popup__name").html(players[playerID].name);
        var playerYear = $(".popup__years").html(players[playerID].year);
        var playerDesc = $(".popup__description").html(players[playerID].highlights);
        var playerStats = $(".popup__stats").html(players[playerID].notablestats);
    });
}

暂无
暂无

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

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