本文介绍了如何在运行时提供元素时在JavaScript中获取innerText?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取元素的 innerText ,但它返回 undefined



code:

 < td id =some-id>< obs conceptId =3156style =textarea/>(此处插入span元素)< / td> 

在运行时生成html:

 < td id =some-id>< span class =value> 14< / span>< / td> 

我曾尝试使用 document.getElementById(some-id)。 innerText 结果是 undefined
我试过使用 document.getElementById(some-id)。innerHTML result是< span class =value> 14 LT; /跨度>



Javascript

b $ b

  var td = document.getElementById('some-id'); 
alert(td.getElementsByTagName('span')[0] .innerHTML);

你的问题是关于 innerText 只有在IE浏览器中,按照我所知,Firefox和Chrome使用





希望它有帮助.. !!


I'm trying to get the innerText of an element but it's returning undefined.

code:

 <td id="some-id"><obs conceptId="3156" style="textarea"/>(this one here inserts the span element)</td>

generated html during runtime:

 <td id="some-id"><span class="value">14</span></td>

I have tried using document.getElementById("some-id").innerText result was undefined.I have tried using document.getElementById("some-id").innerHTML result was <span class="value">14</span>. I would like to get the value 14.

解决方案

Demo Fiddle
Javascript

var td = document.getElementById('some-id');
alert(td.getElementsByTagName('span')[0].innerHTML);  

And your question about innerText it will work only in IE, as per I know Firefox and Chrome uses textContent

Fiddle with textContent

Hope it helps..!!

这篇关于如何在运行时提供元素时在JavaScript中获取innerText?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:21