我正在编写可应用于网页的代码。我希望得到" A"作为变量。我试过了:
document.getElementById("inProgressGrade");
和
document.getElementsByClassName("b");
以下是我提取的代码,为了使其更复杂,我想要获得的所有值都具有相同的类。
<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"));
&#13;
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;" class="inProgressGrade">87.10% <b>B</b></td>
&#13;
console.log(document.getElementsByClassName("inProgressGrade"));
&#13;
<td style="width:60px; border: 1px solid #ABABAB; padding: 2px;" class="inProgressGrade">87.10% <b>B</b></td>
&#13;
答案 0 :(得分:1)
您可以将.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>
答案 1 :(得分:1)
document.querySelectorAll
将成为你的朋友。您需要识别b
下面的所有.inProgressGrade
元素。您还必须使用循环来遍历所有元素,因为有多个元素。
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]);
}
&#13;
<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>
&#13;
答案 2 :(得分:1)
comments = sorted(comments, key=lambda k: self.unix_time_millis(k['time_created']), reverse=True)
@staticmethod
def unix_time_millis(dt):
epoch = datetime.datetime.utcfromtimestamp(0)
return (dt - epoch).total_seconds() * 1000.0
返回一个数组,因此你必须遍历它。之后,您需要做的只是使用.getElementsByClassName()
来获取其“值”。
.innerText
(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}>`);
}
})()