获得儿童元素的价值

时间:2017-12-14 21:19:15

标签: javascript html console.log getelementsbytagname getelementsbyclassname

我正在编写可应用于网页的代码。我希望得到" 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>

&#13;
&#13;
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;
&#13;
&#13;

&#13;
&#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;
&#13;
&#13;

3 个答案:

答案 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元素。您还必须使用循环来遍历所有元素,因为有多个元素。

&#13;
&#13;
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;
&#13;
&#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}>`);
  }
})()