[英]How to return textcontent from tags in DOM in Javascript
我有以下代码:
<table>
<tbody style="font-size: 50px;">
<tr>
<td style="color: #ff00ff; background-color: #ffffff;">Q</td>
<td style="color: #442244; background-color: #442244;">Y</td>
<td style="color: #ffff00; background-color: #442244;">A</td>
</tr>
<tr>
<td style="color: #ffeefe; background-color: #990000;">Q</td>
<td style="color: #ffff00; background-color: #ffff00;">M</td>
<td style="color: #000000; background-color: #ff7777;">O</td>
</tr>
</tbody>
</table>
我必须在 Javasript 中编写 function ,它返回数组中的所有字母。 然后我必须返回所有字母
颜色 == 背景颜色
Array.from(document.getElementsByTagName("td")).map(cell => cell.innerText)
Array.from(document.getElementsByTagName("td")).filter(cell => cell.style.color && cell.style.backgroundColor && cell.style.color === cell.style.backgroundColor).map(cell => cell.innerText);
您可以使用document.getElementByTagName
获取tds
数组,然后根据颜色 == 背景颜色将这些元素reduce
为字母:
const letters = [...document.getElementsByTagName('td')].reduce((a, el) => ( el.style.color === el.style.backgroundColor && a.push(el.textContent), a ), []) console.log(letters)
<table> <tbody style="font-size: 50px;"> <tr> <td style="color: #ff00ff; background-color: #ffffff;">Q</td> <td style="color: #442244; background-color: #442244;">Y</td> <td style="color: #ffff00; background-color: #442244;">A</td> </tr> <tr> <td style="color: #ffeefe; background-color: #990000;">Q</td> <td style="color: #ffff00; background-color: #ffff00;">M</td> <td style="color: #000000; background-color: #ff7777;">O</td> </tr> </tbody> </table>
这个 function 波纹管可以做你需要的:
function getLetters() { const tds = Array.from(document.querySelectorAll("td")); return tds.filter(td => td.style.color === td.style.backgroundColor).map(td => td.textContent); } const letters = getLetters(); console.log(letters)
<table> <tbody style="font-size: 50px;"> <tr> <td style="color: #ff00ff; background-color: #ffffff;">Q</td> <td style="color: #442244; background-color: #442244;">Y</td> <td style="color: #ffff00; background-color: #442244;">A</td> </tr> <tr> <td style="color: #ffeefe; background-color: #990000;">Q</td> <td style="color: #ffff00; background-color: #ffff00;">M</td> <td style="color: #000000; background-color: #ff7777;">O</td> </tr> </tbody> </table>
const hexToRgb = hex => 'rgb(' + hex.match(/[\da-f]{2}/g).map(c => parseInt(c, 16)).join(', ') + ')'; let charsWithBackgroundColor = backgroundColor => [...document.querySelectorAll('td')].filter(td => td.style.backgroundColor === hexToRgb(backgroundColor)).map(td => td.textContent); console.log(charsWithBackgroundColor('#442244')); // ['Y', 'A']
<table> <tbody style="font-size: 50px;"> <tr> <td style="color: #ff00ff; background-color: #ffffff;">Q</td> <td style="color: #442244; background-color: #442244;">Y</td> <td style="color: #ffff00; background-color: #442244;">A</td> </tr> <tr> <td style="color: #ffeefe; background-color: #990000;">Q</td> <td style="color: #ffff00; background-color: #ffff00;">M</td> <td style="color: #000000; background-color: #ff7777;">O</td> </tr> </tbody> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.