“.innerHTML”之间的区别和“.value”在JS中 [英] Difference between ".innerHTML" and ".value" in JS

查看:97
本文介绍了“.innerHTML”之间的区别和“.value”在JS中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JavaScript中的 .innerHTML .value 之间的区别感到困惑。这是我的代码:

I am confused on what is the difference between .innerHTML and .value in JavaScript. Here is my code:

<body>
Input string: <input type="text" id="input" />
....
</body>

当我使用这段代码时,我无法获得输入字符串的内容:

When I use this code I cannot get the content of input string:

var str=document.getElementById("input").innerHTML;

虽然我使用以下代码,但它可以工作:

While I use the following code, it works:

var str=document.getElementById("input").value;

任何人都知道它们之间的区别是什么?

Any one knows what is the difference between them?

推荐答案

value 指的是输入元素(或textearea)的值

value refers to the value of an input element (or textearea)

<input value="hello world">

值将是hello world(或者在里面键入的任何值)

value would be "hello world" (or any value typed inside)

innerHTML 指的是内容在HTML元素中。

innerHTML refers to the contents inside an HTML element.

<div>
  <span class="hello">
     All tags and their children are include in innerHTML.
  </span>
  All this is part of innerHTML.
</div>

div标签的innerHTML将是字符串:

innerHTML of the div tag would be the string:

  '<span class="hello">
     All tags and their children are include in innerHTML.
  </span>
  All this is part of innerHTML.'

这篇关于“.innerHTML”之间的区别和“.value”在JS中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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