[英]How can I get this data-tag-id from this table?
I've ran into a problem while coding.我在编码时遇到了问题。 I have pretty big table with a lot of rows.
我有很多行的大桌子。 I'm getting data from this table using this piece of code:
我正在使用这段代码从该表中获取数据:
let tag1 = document.querySelector('body > div.l-global-width.l-container-primary > div > div.l-container-col2.box-userprofile > section > table > tbody > tr:nth-child(1) > td:nth-child(2)').innerText;
The problem is, i need to get id of this tag from this table.问题是,我需要从这个表中获取这个标签的 id。
<tr data-tag-id="7" role="row">
<td class="ord">1</td>
<td>Komedia</td>
<td class="number">2</td>
<td class="number">10</td>
<td class="number">131</td>
<td class="number" data-sort-value="7.44">7,44</td>
<td class="number" data-sort-value="7.21">7,21</td>
<td class="number" data-sort-value="34434">573h 54m</td>
</tr>
..and as you can guess, I have no idea how to do it. ..你可以猜到,我不知道该怎么做。 I've tried a few things, but none of them worked for me, and I'm not experienced enough to come with another ideas.
我尝试了一些东西,但没有一个对我有用,而且我没有足够的经验来提出其他想法。
Can anyone help me solve it?任何人都可以帮我解决它吗? I appreciate all the help.
我感谢所有的帮助。
Use the attribute selector []
:使用属性选择器
[]
:
const ELS_tagId = document.querySelectorAll("tr[data-tag-id]");
which will give you a collection of all <tr>
s with data-tag-id
elements.这将为您提供所有带有
data-tag-id
元素的<tr>
的集合。
To loop them:循环它们:
ELS_tagId.forEach(function(el) {
console.log(el.dataset.tagId); // among which you'll get also that "7"
});
PS: notice that dash-separated HTML data-* tags are used as camelCase in JS tag-id
→ tagId
PS:注意用破折号分隔的 HTML data-* 标签在 JS
tag-id
→ tagId
中被用作 camelCase
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.