我正在尝试将下面的 JQuery
代码转换为原始 Javascript
版本,但卡在了循环中。这是我目前的尝试:
Javascript:
var x = document.getElementById("Table1"), d = x.getElementByTagName("td");
for (let row of x.rows)
{
for(let cell of row.cells)
{
var td_content = $(this).text();
if (td_content != "") {
hide = false;
}
}
}
下面是我尝试转换的原始 JQuery
代码。
JQuery:
$('#Table1 td').each(function()
{
var td_content = $(this).text();
if (td_content != "") {
hide = false;
}
})
最佳答案
我会使用 document.querySelectorAll
来轻松转换为 vanilla JS。
var hide = true;
var cells = document.querySelectorAll('#Table1 td');
cells.forEach(function(cell) {
if (cell.innerText !== '') {
hide = false;
}
});
console.log(hide);
关于javascript - 如何将 JQuery 转换为 vanilla Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56349633/