最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - innerHTML does not work on span tag - Stack Overflow

programmeradmin1浏览0评论

I have the next chunk of html on my html doc:

<p class="load-chart">Carregando Grafico<span id="load-dots">.</span></p>

When I try to use innerHTML on I get the undefined msg, however when I use:

$('#load-dots').text("any new text");

it works fine.

How I fixed

i solved this issue using:

$('span#load-dots').get(0).innerHTML;

I have the next chunk of html on my html doc:

<p class="load-chart">Carregando Grafico<span id="load-dots">.</span></p>

When I try to use innerHTML on I get the undefined msg, however when I use:

$('#load-dots').text("any new text");

it works fine.

How I fixed

i solved this issue using:

$('span#load-dots').get(0).innerHTML;
Share Improve this question edited Nov 23, 2016 at 12:50 Natanael asked Nov 23, 2016 at 12:34 NatanaelNatanael 3991 gold badge3 silver badges14 bronze badges 5
  • 1 There is a type innterHTML it should be innerHTML. Please clarify if this is code type or just so question type. – Subir Kumar Sao Commented Nov 23, 2016 at 12:35
  • 1 Sorry, I was a type error here in Stackkoverflow, I already have edit. – Natanael Commented Nov 23, 2016 at 12:37
  • 1 The actual syntax for jQuery is just html() – Subir Kumar Sao Commented Nov 23, 2016 at 12:37
  • 1 Try $('#load-dots').get(0).innerHTML But instead you could use .html – Rajaprabhu Aravindasamy Commented Nov 23, 2016 at 12:38
  • @RajaprabhuAravindasamy the solution with get(0).innerHTML works fine to me, thx :D – Natanael Commented Nov 23, 2016 at 12:48
Add a ment  | 

2 Answers 2

Reset to default 5

innerHTML works fine:

<p class="load-chart">Carregando Grafico<span id="load-dots">.</span></p>

and

document.getElementById("load-dots").innerHTML = "some new text"

See: .innerHTML - JSFiddle

Also if you are already using jQuery, you should use .html. Its not a good practice to mix multiple approaches.

.html() - JSFiddle

Also note that .html or .innerHTML is used to update markup of an element. If you just wish to update text, use .text(). Pure JS alternative would be .textContent

.text() - JSFiddle

Using jQuery one can invoke .html(),

$('#load-dots').html("any new text");
发布评论

评论列表(0)

  1. 暂无评论