我想知道是否有人可以帮助我并解释为什么,当我拆分innerText时,它有时返回undefined
,是否有人可以向我指出正确的解决方法
var elems = document.getElementsByClassName("hud-leaderboard-party"),
counter = elems.length,
arrayLength = 0,
maxLengthArray = 0;
// LEADERBOARD NAMES
window.leaderboardNames = function() {
var Elem;
if (counter <= 0)
counter = elems.length;
Elem = elems[counter - 1];
document.getElementsByClassName("hud-intro-name")[0].value = Elem.innerText.split("—")[0].split(",")[arrayLength];
maxLengthArray = Elem.innerText.split("—")[0].split(",").length;
if (maxLengthArray == arrayLength) {
arrayLength = 0;
counter--;
} else {
arrayLength++;
}
}
<div class="hud-intro-leaderboard">
<div class="hud-intro-leaderboard-parties">
<div class="hud-leaderboard-party">Player name here 1, Player name here 2, Player name here 3, Player name here 4 — <strong>998</strong></div>
<div class="hud-leaderboard-party">Player name here 5, Player name here 6, Player name here 7 — <strong>707</strong></div>
<div class="hud-leaderboard-party">Player name here 8, Player name here 9, Player name here 10 — <strong>610</strong></div>
<div class="hud-leaderboard-party">Player name here 11, Player name here 12 — <strong>448</strong></div>
<div class="hud-leaderboard-party">Player name here 13, Player name here 14, Player name here 15, Player name here 16 — <strong>409</strong></div>
</div>
</div>
<br><br>
<button class="btn btn-green" style="width: 90%;" onclick="leaderboardNames();">LEADERBOARD NAMES</button>
<br><br>
<input type="text" class="hud-intro-name" style="width: 90%;" maxlength="29" placeholder="Your nickname">
答案 0 :(得分:0)
如果-后面的值不具有,则会发生未定义的情况,
所以在声明中
Elem.innerText.split("—")[0].split(",")[arrayLength];
您需要先检查
Elem.innerText.split("—")[0].split(",")
并安全地从中提取。
答案 1 :(得分:0)
在您的arrayLength检查中发出问题,需要为+1
let elems = document.getElementsByClassName("hud-leaderboard-party"),
counter = elems.length,
arrayLength = 0,
maxLengthArray = 0;
// LEADERBOARD NAMES
window.leaderboardNames = function() {
let elem;
if (counter <= 0) {
console.log(counter);
counter = elems.length;
}
elem = elems[counter - 1];
let elemNames = elem.innerText.split("—")[0].split(',');
document.querySelector(".hud-intro-name").value = elemNames[arrayLength];
maxLengthArray = elem.innerText.split("—")[0].split(",").length;
if (maxLengthArray == arrayLength+1) {
arrayLength = 0;
counter--;
} else {
arrayLength++;
}
}
<div class="hud-intro-leaderboard">
<div class="hud-intro-leaderboard-parties">
<div class="hud-leaderboard-party">⋘TC⋙ ƘƠƦƘƲƧƲȤ, ƬƇ☢☠ⓀⓊⓇⓉ☠☢, ⋘TC⋙ ƘƠƦƘƲƧƲȤ, ƬƇ☢☠ⓀⓊⓇⓉ☠☢ — <strong>998</strong></div>
<div class="hud-leaderboard-party">Sweet Heart TW, High Score GO!, High Score GO! — <strong>707</strong></div>
<div class="hud-leaderboard-party">K¥LË, K¥LË3, This has been removed, This has been removed — <strong>610</strong></div>
<div class="hud-leaderboard-party">Exxotic, ภคŦғเค :P... (MSR), Hauzz✔️ — <strong>448</strong></div>
<div class="hud-leaderboard-party">Brightstar, Yaslin, Yaslin, Yaslin — <strong>409</strong></div>
</div>
</div>
<br><br>
<button class="btn btn-green" style="width: 90%;" onclick="leaderboardNames();">LEADERBOARD NAMES</button>
<br><br>
<input type="text" class="hud-intro-name" style="width: 90%;" maxlength="29" placeholder="Your nickname">