简体   繁体   English

无 html 标记的 html 元素的普通 javascript 选择器

[英]Plain javascript selector for html element without html markup

I'm working on a small project where I'd to read how many times a monster has been killed by a specific player.我正在做一个小项目,我会在其中读取特定玩家杀死怪物的次数。 I'm able to create most of the selectors (monster name, player name) when using the document.querySelector or document.querySelectorAll.使用 document.querySelector 或 document.querySelectorAll 时,我可以创建大部分选择器(怪物名称、玩家名称)。 But when I want to retrieve the amount of kills I'm stuck.但是当我想检索击杀数量时,我被卡住了。 Also Googling for selectors of plain text, or text without HTML markup.还搜索纯文本选择器或没有 HTML 标记的文本。 Unfortunately I have no results.不幸的是我没有结果。

To give you an idea how it looks like I will give a small example.为了让您了解它的外观,我将举一个小例子。 The HTML looks exactly like this, but then you have 6 player names list and for each player the amount of kills for this specific monster. HTML 看起来完全像这样,但是你有 6 个玩家姓名列表,每个玩家都有这个特定怪物的击杀数量。

<table>
<tr>
<td><b>Name of the monster</b></td>
<td><b><a href="#">Name of the player</a></b>"Amount of kills"<br></td>
</tr>
</table>

So I want to get the selector of the text that mentions the Amount of kills.所以我想得到提到杀戮数量的文本的选择器。 I will then convert this string to a number.然后我会将此字符串转换为数字。

My idea is to create a tracker to see what players are actually hunting.我的想法是创建一个跟踪器来查看玩家实际在狩猎什么。 For example player X kills 500 dragons, then I want my script to display this.例如玩家 X 杀死了 500 条龙,那么我希望我的脚本显示这个。

Thanks in advance: :)提前致谢: :)

Here's one way:这是一种方法:

 const cell = document.querySelector('table tr td:last-child'); console.log([...cell.childNodes].filter(n=>n.nodeType===3).pop().nodeValue);
 <table> <tr> <td><b>Name of the monster</b></td> <td><b><a href="#">Name of the player</a></b>"Amount of kills"<br></td> </tr> </table>

It finds the table cell you need, and reads all childNodes (this includes textNodes (nodeType is 3 then)), filters for all textNodes and pops the last one, reading its value.它找到您需要的表格单元格,并读取所有 childNodes(这包括 textNodes(此时 nodeType 为 3)),过滤所有 textNodes 并弹出最后一个,读取其值。

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

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