I am currently using a bunch of input textfields and I want to change it to a DIV, but most of my JS functions use document.getElementById("inputField1").value
whenever the value of the input field is set like this:
目前我正在使用大量的输入文本字段,我想将其更改为DIV,但是我的大多数JS函数都使用document.getElementById(“inputField1”)。当输入字段的值设置为:
<input contenteditable="false" id="inputField1" type="text" size="12" style="background:#09F;color:#FFF;text-align:center" value="Hello World!"/>
That would return just Hello World!
if I were to display the value in an alert
那只会返回你好世界!如果我要在警报中显示值
How would I get the value of the text in between if I were to use DIVs?
如果我使用DIVs,如何得到中间文本的值呢?
For example <div id="inField001">Hello World</div>
例如
Thanks!
谢谢!
5 个解决方案
#1
8
In that case you can use:
在这种情况下,你可以使用:
document.getElementById('inField001').innerHTML
Or:
或者:
document.getElementById('inField001').innerText
Or:
或者:
document.getElementById('inField001').textContent
Also (if you want to use jQuery):
同样(如果你想使用jQuery):
$('#inField001').text()
#2
5
You would just do
你只会做
var value = document.getElementById('inField001').innerHTML);
But if your DIV has some html this will grab that too.
但是如果你的DIV有一些html,它也会抓取那个。
.innerHTML
You can also use document.getElementById('inField001').textContent)
to grab just the text nodes from the element ignoring any element wrappers.
您还可以使用document.getElementById('inField001'). textcontent)从元素中获取文本节点,而忽略任何元素包装器。
But support for textContent is not as good as innerHTML. See doc for info and support.
但是对textContent的支持不如innerHTML那么好。有关信息和支持,请参阅doc。
Another way is using innerText
. alert(document.getElementById('inField001').innerText);
but not supported in FF.
另一种方法是使用内文。警报(. getelementbyid(“inField001”).innerText);但没有FF支持。
See Doc for support.
看到医生的支持。
Fiddle
#3
1
Use the innerHTML property.
使用innerHTML属性。
document.getElementById("inField001").innerHTML
BTW this kind of thing is way better to do with jQuery.
顺便说一句,使用jQuery更好。
#4
0
For just text content:
对文本内容:
var value = document.getElementById('inputField1').textContent;
For the more verbose version, see here.
有关更详细的版本,请参见这里。
#5
-1
or just do
或者只是做
x = document.getElementById("inField001");
alert(x);
#1
8
In that case you can use:
在这种情况下,你可以使用:
document.getElementById('inField001').innerHTML
Or:
或者:
document.getElementById('inField001').innerText
Or:
或者:
document.getElementById('inField001').textContent
Also (if you want to use jQuery):
同样(如果你想使用jQuery):
$('#inField001').text()
#2
5
You would just do
你只会做
var value = document.getElementById('inField001').innerHTML);
But if your DIV has some html this will grab that too.
但是如果你的DIV有一些html,它也会抓取那个。
.innerHTML
You can also use document.getElementById('inField001').textContent)
to grab just the text nodes from the element ignoring any element wrappers.
您还可以使用document.getElementById('inField001'). textcontent)从元素中获取文本节点,而忽略任何元素包装器。
But support for textContent is not as good as innerHTML. See doc for info and support.
但是对textContent的支持不如innerHTML那么好。有关信息和支持,请参阅doc。
Another way is using innerText
. alert(document.getElementById('inField001').innerText);
but not supported in FF.
另一种方法是使用内文。警报(. getelementbyid(“inField001”).innerText);但没有FF支持。
See Doc for support.
看到医生的支持。
Fiddle
#3
1
Use the innerHTML property.
使用innerHTML属性。
document.getElementById("inField001").innerHTML
BTW this kind of thing is way better to do with jQuery.
顺便说一句,使用jQuery更好。
#4
0
For just text content:
对文本内容:
var value = document.getElementById('inputField1').textContent;
For the more verbose version, see here.
有关更详细的版本,请参见这里。
#5
-1
or just do
或者只是做
x = document.getElementById("inField001");
alert(x);