简体   繁体   English

如何<nobr>使用JavaScript而不是jQuery</nobr>获取<nobr>td标签内标签</nobr>的值

[英]How to get the Value for <nobr> tag inside the td tag using javascript not jquery

I am trying the below code to get the values from td tag. 我正在尝试下面的代码从td标签获取值。 I can get the My Name value but i am not able to store the date value which is inside nobr tag. 我可以获取“我的名字”值,但无法存储nobr标签内的日期值。

Below is my logical code. 下面是我的逻辑代码。

var n = tableRow[t].getElementsByClassName("ms-vb2").length;

        for (var i=0, n; i < n; i++) 
        {       
          console.log(tableRow[t].getElementsByClassName("ms-vb2").item(i).firstChild.nodeValue);
        }

<tr>
<td class="ms-vb2"> My Name </td>
<td class="ms-vb2">
<nobr> 5/31/217 </nobr>
</td>
</tr>

Consider using the textContent and innerText properties. 考虑使用textContentinnerText属性。

 var cells = document.querySelectorAll(".ms-vb2"); for (var i = 0, n = cells.length; i < n; i++) { var text = cells[i].textContent ? cells[i].textContent : cells[i].innerText; console.log(text); } 
 <table> <tr> <td class="ms-vb2"> My Name </td> <td class="ms-vb2"> <nobr> 5/31/217 </nobr> </td> </tr> </table> 

Note that IE8 (as well as IE9, 10, and 11 when running in compatibility mode) will use innerText , while other browsers will use textContent . 请注意,IE8(以及在兼容模式下运行的innerText和11)将使用innerText ,而其他浏览器将使用textContent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM