使用属性选择器:
https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors
console.log(document.querySelector('[data=chair]').textContent);
<table>
<tr>
<td class="main" data="chair">Chair</td>
<td class="main" data="table">Table</td>
<td class="main" data="desk">Desk</td>
</tr>
</table>
你能试试这个代码吗?
function exampleTable() {
var getTables = document.querySelectorAll(".main");
getTables.forEach((cell) => {
var content = cell.textContent;
var dataAttribute = cell.getAttribute('data');
console.log(content);
console.log(dataAttribute);
});}