更改Html标签的innerText [英] Changing the innerText of a Html Label

查看:144
本文介绍了更改Html标签的innerText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个非常简单的html / js代码页:



I have this very simple html/js code page :

<html>
<head>
   <title>Prova !1</title>
   <script>
      function test (a)
      {
         var v = 5;
         v = !1;		
         return v;
      }

   </script>
</head>
<body>

   <label id='2'> the answer is</label>
   <script>
      window.onload = function ()
      {
         var i ;
         i = document.getElementById('2');
         i[0].innerText += " "+test (0);
      }
   </script>
</body>
</html>







任何人都可以解释为什么,无论我使用什么样的物体,都可以它是一个标签或其他, document.getElementbyId('2')总是返回 null

推荐答案

查看我的示例和评论:

See my example and comments:
<html>
<head>
   <title>Prova !1</title>
   <script>
      function test (a) // a has never used???
      {
         var v = 5;
         v = !1; // v is always false
         return v;  // returns false
      }

   </script>
</head>
<body>

   <label id='2'> the answer is</label>
   <script>
      window.onload = function ()
      {
         var i ;
         i = document.getElementById('2');
         //i[0].innerText += " "+test (0);
         i.innerHTML += " "+test(0); // the outcome is : the answer is false
      }
   </script>
</body>
</html>



你想用这个奇怪的代码做什么?


What exactly do you want to do with this weird code?


彼得是对的。你遇到了问题...

Peter is correct. You had problems with...
i[0].innerText += " " + test (0);



哪个应该是...


Which should be...

i.innerHTML += " " + test(0);





演示



[问题]更改Html标签的内部文本 [ ^ ]。



注意



如他所说,想一想首先是代码,因为它完全没有任何意义。



Demo

[Issue] Changing the innerText of a Html Label[^].

Note

As he says, think over the code first, because it is making no sense at all.


这篇关于更改Html标签的innerText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆