Javascript 获取 DIV 标签之间的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3027439/
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
Get values between DIV tags?
提问by 001
How do I get the values in between a DIV tag?
如何获取 DIV 标签之间的值?
Example
例子
<div id="myOutput" class="wmd-output">
<pre><code><p>hello world!</p></code></pre>
</div>
my output values I should get is
我应该得到的输出值是
<pre><code><p>hello world!</p></pre>
回答by meagar
回答by programble
document.getElementById("myOutput").innerHTML
回答by dzida
innerHtml is good for this case as guys suggested before me,
正如我之前建议的那样,innerHtml 对这种情况很有用,
If you have more complex html structure and want to traverse/manipulate it I suggest to use js libraries like jQuery. To get want you want it would be:
如果您有更复杂的 html 结构并想要遍历/操作它,我建议使用 js 库,如jQuery。要得到你想要的,它会是:
$('#myOutput').html()
Looks nicer I think (but I wouldn't load whole js library just for such simple example of course)
我认为看起来更好(但我当然不会为了这样简单的例子加载整个 js 库)
回答by Umesh Aawte
Just putting all above with some additional details,
只是把上面的所有内容加上一些额外的细节,
If you are not sure about that div having some id is not there on html page then to make it sure please use.
如果您不确定 html 页面上不存在具有某些 id 的 div,请确保使用。
var objDiv = document.getElementbyId('myOutput');
if(objDiv){
objDiv.innerHTML;
}
This will avoid any JavaScript error on the page.
这将避免页面上出现任何 JavaScript 错误。

