Javascript 将innerhtml值放入变量中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6492671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting innerhtml value into the variable
提问by sateesh
I am getting a value by using document.getElementById("forloop").innerHtml
. And I am displaying that value in the file with some div id as
我通过使用获得了一个价值document.getElementById("forloop").innerHtml
。我在文件中显示该值,其中包含一些 div id
<div id="forloop"></div>.
But I want to assign that value to a variable in the file. Can you please how can I assign to the variable?
但我想将该值分配给文件中的一个变量。请问我如何分配给变量?
回答by FishBasketGordo
var myInnerHtml = document.getElementById("forloop").innerHTML;
In most browsers, the property is named innerHTML
(with HTML in all-caps), not innerHtml, so watch for that.
在大多数浏览器中,属性被命名innerHTML
(HTML 全部大写),而不是innerHtml,所以要注意这一点。
回答by samach
If you want the value along with the HTML tags then you will use:
如果您想要该值以及 HTML 标签,那么您将使用:
var x=document.getElementById("forloop").innerHTML;
Or if you want only the value then you will use:
或者,如果您只想要该值,那么您将使用:
var x=document.getElementById("forloop").innerText;
回答by daniel.herken
I'm not sure if i understand right, maybe this?
我不确定我是否理解正确,也许是这个?
var variable = document.getElementById("forloop").innerHTML;
回答by Giann
Don't know where the strech is:
不知道拉伸在哪里:
var x = document.getElementById("forloop").innerHTML;