如何使用Javascript从这个table获取信息
How to use Javascript to obtain information from this table
我正在使用 Tampermonkey 编写脚本来自动执行某些操作。
我正在尝试获取某些角色的等级,以便我可以选择最低等级,然后单击它以select它。
相关代码如下:
<td class="box-select boxPk pad-10">
<label for="(NULL)">
<input class="hidden" type="radio" name="replacement" id="(NULL)" value="(NULL)">
<b><a href="#" onclick="pokedexTab('pid=(NULL)', 1); return false;">Robert</a></b> <i class="ion-female female"></i><br>
<img src="(image url)"><br>
<b class="color-maroon">Level:</b> 16<br>
<b class="color-maroon">Exp:</b> 8,000
</label>
</td>
我正在尝试获取某些角色的等级,以便我可以选择最低等级,然后单击它以select它。
整个 td 标签都是可点击的,在 table 中有很多类似的标签,每个标签都有不同的属性。
忽略 (NULL),我只是将其替换为唯一 ID。
非常感谢您阅读此问题并感谢您提供的任何可能的帮助!
试试这个 - 假设单元格中只有一个 Level:
let levels = [];
const $levelCells=$(".boxPk>label");
$levelCells.each(function() {
console.log($(this).find("a").text()); // if you want the name you need to save it
levels.push(parseInt($(this).text().split("Level: ")[1]));
})
const maxNum = Math.max(...levels) // 16
const minNum = Math.min(...levels) // 14
console.log("Min",minNum);
console.log("Max",maxNum);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td class="box-select boxPk pad-10">
<label for="(NULL)">
<input class="hidden" type="radio" name="replacement" id="(NULL)" value="(NULL)">
<b><a href="#" onclick="pokedexTab('pid=(NULL)', 1); return false;">Robert</a></b> <i class="ion-female female"></i><br>
<img src="(image url)"><br>
<b class="color-maroon">Level:</b> 14<br>
<b class="color-maroon">Exp:</b> 8,000
</label>
</td>
<td class="box-select boxPk pad-10">
<label for="(NULL)">
<input class="hidden" type="radio" name="replacement" id="(NULL)" value="(NULL)">
<b><a href="#" onclick="pokedexTab('pid=(NULL)', 1); return false;">Frank</a></b> <i class="ion-female female"></i><br>
<img src="(image url)"><br>
<b class="color-maroon">Level:</b> 15<br>
<b class="color-maroon">Exp:</b> 8,000
</label>
</td>
<td class="box-select boxPk pad-10">
<label for="(NULL)">
<input class="hidden" type="radio" name="replacement" id="(NULL)" value="(NULL)">
<b><a href="#" onclick="pokedexTab('pid=(NULL)', 1); return false;">Fred</a></b> <i class="ion-female female"></i><br>
<img src="(image url)"><br>
<b class="color-maroon">Level:</b> 16<br>
<b class="color-maroon">Exp:</b> 8,000
</label>
</td>
</tr>
</table>
我正在使用 Tampermonkey 编写脚本来自动执行某些操作。
我正在尝试获取某些角色的等级,以便我可以选择最低等级,然后单击它以select它。
相关代码如下:
<td class="box-select boxPk pad-10">
<label for="(NULL)">
<input class="hidden" type="radio" name="replacement" id="(NULL)" value="(NULL)">
<b><a href="#" onclick="pokedexTab('pid=(NULL)', 1); return false;">Robert</a></b> <i class="ion-female female"></i><br>
<img src="(image url)"><br>
<b class="color-maroon">Level:</b> 16<br>
<b class="color-maroon">Exp:</b> 8,000
</label>
</td>
我正在尝试获取某些角色的等级,以便我可以选择最低等级,然后单击它以select它。
整个 td 标签都是可点击的,在 table 中有很多类似的标签,每个标签都有不同的属性。
忽略 (NULL),我只是将其替换为唯一 ID。
非常感谢您阅读此问题并感谢您提供的任何可能的帮助!
试试这个 - 假设单元格中只有一个 Level:
let levels = [];
const $levelCells=$(".boxPk>label");
$levelCells.each(function() {
console.log($(this).find("a").text()); // if you want the name you need to save it
levels.push(parseInt($(this).text().split("Level: ")[1]));
})
const maxNum = Math.max(...levels) // 16
const minNum = Math.min(...levels) // 14
console.log("Min",minNum);
console.log("Max",maxNum);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td class="box-select boxPk pad-10">
<label for="(NULL)">
<input class="hidden" type="radio" name="replacement" id="(NULL)" value="(NULL)">
<b><a href="#" onclick="pokedexTab('pid=(NULL)', 1); return false;">Robert</a></b> <i class="ion-female female"></i><br>
<img src="(image url)"><br>
<b class="color-maroon">Level:</b> 14<br>
<b class="color-maroon">Exp:</b> 8,000
</label>
</td>
<td class="box-select boxPk pad-10">
<label for="(NULL)">
<input class="hidden" type="radio" name="replacement" id="(NULL)" value="(NULL)">
<b><a href="#" onclick="pokedexTab('pid=(NULL)', 1); return false;">Frank</a></b> <i class="ion-female female"></i><br>
<img src="(image url)"><br>
<b class="color-maroon">Level:</b> 15<br>
<b class="color-maroon">Exp:</b> 8,000
</label>
</td>
<td class="box-select boxPk pad-10">
<label for="(NULL)">
<input class="hidden" type="radio" name="replacement" id="(NULL)" value="(NULL)">
<b><a href="#" onclick="pokedexTab('pid=(NULL)', 1); return false;">Fred</a></b> <i class="ion-female female"></i><br>
<img src="(image url)"><br>
<b class="color-maroon">Level:</b> 16<br>
<b class="color-maroon">Exp:</b> 8,000
</label>
</td>
</tr>
</table>