获取子元素的价值
Getting Value of Child Element
我正在编写可应用于网页的代码。我想获取 "A" 的值作为变量。我试过:
document.getElementById("inProgressGrade");
和
document.getElementsByClassName("b");
这是我从中提取的代码,使它更复杂我想要获得的所有值都具有相同的 class。
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;" class="inProgressGrade">
90.62%
<b>A</b>
</td>
同一网页上的另一个:
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;" class="inProgressGrade">
87.10%
<b>B</b>
</td>
console.log(document.getElementsByClassName("b"));
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;" class="inProgressGrade">87.10% <b>B</b></td>
console.log(document.getElementsByClassName("inProgressGrade"));
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;" class="inProgressGrade">87.10% <b>B</b></td>
您可以将 .querySelector()
与 textContent
一起使用,就像:
document.querySelector('.inProgressGrade b').textContent
console.log(document.querySelector('.inProgressGrade>b').textContent);
<table>
<tr>
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;" class="inProgressGrade">
90.62%
<b>A</b>
</td>
</tr>
</table>
如果你想要这两个字母,你可以使用 .querySelectorAll()
循环,如:
var letters = document.querySelectorAll('.inProgressGrade b');
for( var i = 0; i < letters.length; i++) {
console.log( letters[i].textContent );
}
代码:
var letters = document.querySelectorAll('.inProgressGrade b');
for (var i = 0; i < letters.length; i++) {
console.log(letters[i].textContent);
}
<table>
<tr>
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;" class="inProgressGrade">
90.62%
<b>A</b>
</td>
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;" class="inProgressGrade">
87.10%
<b>B</b>
</td>
</tr>
</table>
document.querySelectorAll
将成为您的朋友。您将需要识别 .inProgressGrade
下面的所有 b
元素。您还必须使用循环遍历所有元素,因为有多个元素。
var els = document.querySelectorAll(".inProgressGrade b");
var vals = [];
for(var i in els) {
if (els[i] && els[i].innerText)
vals.push(els[i].innerText);
}
for(var i in vals) {
console.log(vals[i]);
}
<table id="myTable">
<tbody>
<tr>
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;"
class="inProgressGrade">
90.62%
<b>A</b>
</td>
</tr>
<tr>
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;"
class="inProgressGrade">
87.10%
<b>B</b>
</td>
</tr>
</tbody>
</table>
.getElementsByClassName()
return 一个数组,因此你必须遍历它。之后你需要做的就是使用 .innerText
得到它的 "value".
(function() {
const allTds = document.getElementsByClassName('inProgressGrade'); //get all td-elements for which you want the value
var values = [];
for (let i = 0; i < allTds.length; ++i) {
const bEl = allTds[i].getElementsByTagName('b')[0]; // takes the first b-element, you'll need another loop if there are multiple
/* Since <b> is an inline element it will be part of its parents innerText */
const textWithChild = allTds[i].innerText;
const text = textWithChild.substring(0, textWithChild.length - bEl.innerText.length - 1);
console.log(`The value of ${bEl.innerText} is <${text}>`);
}
})()
<table>
<tr>
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;" class="inProgressGrade">
90.62%
<b>A</b>
</td>
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;" class="inProgressGrade">
87.10%
<b>B</b>
</td>
</tr>
</table>
我正在编写可应用于网页的代码。我想获取 "A" 的值作为变量。我试过:
document.getElementById("inProgressGrade");
和
document.getElementsByClassName("b");
这是我从中提取的代码,使它更复杂我想要获得的所有值都具有相同的 class。
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;" class="inProgressGrade">
90.62%
<b>A</b>
</td>
同一网页上的另一个:
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;" class="inProgressGrade">
87.10%
<b>B</b>
</td>
console.log(document.getElementsByClassName("b"));
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;" class="inProgressGrade">87.10% <b>B</b></td>
console.log(document.getElementsByClassName("inProgressGrade"));
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;" class="inProgressGrade">87.10% <b>B</b></td>
您可以将 .querySelector()
与 textContent
一起使用,就像:
document.querySelector('.inProgressGrade b').textContent
console.log(document.querySelector('.inProgressGrade>b').textContent);
<table>
<tr>
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;" class="inProgressGrade">
90.62%
<b>A</b>
</td>
</tr>
</table>
如果你想要这两个字母,你可以使用 .querySelectorAll()
循环,如:
var letters = document.querySelectorAll('.inProgressGrade b');
for( var i = 0; i < letters.length; i++) {
console.log( letters[i].textContent );
}
代码:
var letters = document.querySelectorAll('.inProgressGrade b');
for (var i = 0; i < letters.length; i++) {
console.log(letters[i].textContent);
}
<table>
<tr>
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;" class="inProgressGrade">
90.62%
<b>A</b>
</td>
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;" class="inProgressGrade">
87.10%
<b>B</b>
</td>
</tr>
</table>
document.querySelectorAll
将成为您的朋友。您将需要识别 .inProgressGrade
下面的所有 b
元素。您还必须使用循环遍历所有元素,因为有多个元素。
var els = document.querySelectorAll(".inProgressGrade b");
var vals = [];
for(var i in els) {
if (els[i] && els[i].innerText)
vals.push(els[i].innerText);
}
for(var i in vals) {
console.log(vals[i]);
}
<table id="myTable">
<tbody>
<tr>
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;"
class="inProgressGrade">
90.62%
<b>A</b>
</td>
</tr>
<tr>
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;"
class="inProgressGrade">
87.10%
<b>B</b>
</td>
</tr>
</tbody>
</table>
.getElementsByClassName()
return 一个数组,因此你必须遍历它。之后你需要做的就是使用 .innerText
得到它的 "value".
(function() {
const allTds = document.getElementsByClassName('inProgressGrade'); //get all td-elements for which you want the value
var values = [];
for (let i = 0; i < allTds.length; ++i) {
const bEl = allTds[i].getElementsByTagName('b')[0]; // takes the first b-element, you'll need another loop if there are multiple
/* Since <b> is an inline element it will be part of its parents innerText */
const textWithChild = allTds[i].innerText;
const text = textWithChild.substring(0, textWithChild.length - bEl.innerText.length - 1);
console.log(`The value of ${bEl.innerText} is <${text}>`);
}
})()
<table>
<tr>
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;" class="inProgressGrade">
90.62%
<b>A</b>
</td>
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;" class="inProgressGrade">
87.10%
<b>B</b>
</td>
</tr>
</table>