如何获得div的内部html [英] How to get inner html of div

查看:62
本文介绍了如何获得div的内部html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了表运行时间,它在Div.And中我点击按钮后我想要div的内部HTML但它给我错误无法得到Div表的内部内容,因为内容不是文字。



我该怎么办?

解决方案

在javascript中你可以这样试试



 document.getElementById('Divname')。innerHTML; 


我试图重现你的错误使用两种方法在div中创建表。我只发现了一个区别:在使用innerHTML设置表构造的IE中,可以省略TBODY标记。它将由IE自己创建,但是当您通过createElement和appendChild创建表时,必须包含TBODY标记,否则表格将不会显示。 FF适用于两种变体。



足够说,这是证实我声称的代码:



 <   html  > ;  
< head >

< script 类型 = text / javascript >
function buildTableWithInnerHTML()
{
var c = document.getElementById(container1 );
c.innerHTML =< table > < tr > < td > 1 < / td > < span class =code-keyword>< td > 2 < / td > < td > 3 < / td > < / tr > < tr > < td > 4 < / td > < td > 5 < / td > < td > 6 < / td > < / tr > < / table > ;
}

函数buildTableWithCreateElement()
{
var d = document;
var c = d.getElementById(container2);
var t = d.createElement(table);
var tb = d.createElement(tbody);

c.appendChild(t);
t.appendChild(tb);

var r1 = d.createElement(tr);
var r2 = d.createElement(tr);

tb.appendChild(r1);
tb.appendChild(r2);

var d11 = d.createElement(td);
var d12 = d.createElement(td);
var d13 = d.createElement(td);
var d21 = d.createElement(td);
var d22 = d.createElement(td);
var d23 = d.createElement(td);
d11.appendChild(d.createTextNode(1));
d12.appendChild(d.createTextNode(2));
d13.appendChild(d.createTextNode(3));
d21.appendChild(d.createTextNode(4));
d22.appendChild(d.createTextNode(5));
d23.appendChild(d.createTextNode(6));
r1.appendChild(d11);
r1.appendChild(d12);
r1.appendChild(d13);
r2.appendChild(d21);
r2.appendChild(d22);
r2.appendChild(d23);
}
< / script >

< script 类型 = text / javascript >

function ShowDivInnerHTML()
{
var c = document .getElementById( 'container1');
alert(由innerHTML创建:\ n+ c.innerHTML);
c = document.getElementById('container2');
alert(由create& append创建:\ n+ c.innerHTML);
}

< / script >

< title > DIV中的动态表< / title >
< / head >
< 正文 onload = buildTableWithCreateElement(); buildTableWithInnerHTML(); >
< h2 > G通过设置innerHTML < / h2 >
< div id = container1 > < / div >
< h2 > 通过createElement&生成appendChild < / h2 >
< div id = container2 > < / div >
< 输入 type = 按钮 value = 显示它! onC舔 = ShowDivInnerHTML(); >
< br >
< / body >
< / html >





如果您仍有疑问,请随时发表评论。< br $> b $ b

编码愉快!



-MRB


var sb = new StringBuilder();

mainDiv.RenderControl(new HtmlTextWriter(new StringWriter(sb)));



string s = sb.ToString ();

I have created table run time which is in Div.And after i click on button i want inner HTML of div but it givs me error "Cannot get inner content of Div Table because the contents are not literal".

What should i Do?

解决方案

in javascript you can try like this

document.getElementById('Divname').innerHTML;


I tried to reproduce your error by using two methods of creating the table within the div. I only found out one difference: In IE using innerHTML to set the table construct one can omit the TBODY tag. It will be created by IE on its own, but when you create the table via createElement and appendChild you MUST include the TBODY tag otherwise the table wont be shown. FF works happily with both variants.

Enough said, here's the code to corroborate my claim:

<html>
<head>

<script type="text/javascript">
function buildTableWithInnerHTML()
{
    var c = document.getElementById("container1");
    c.innerHTML = "<table><tr><td>1</td><td>2</td><td>3</td></tr><tr><td>4</td><td>5</td><td>6</td></tr></table>";
}

function buildTableWithCreateElement()
{
    var d = document;
    var c = d.getElementById("container2");
    var t  = d.createElement("table");
    var tb = d.createElement("tbody");

    c.appendChild(t);
    t.appendChild(tb);

    var r1 = d.createElement("tr");
    var r2 = d.createElement("tr");

    tb.appendChild(r1);
    tb.appendChild(r2);

    var d11 = d.createElement("td");
    var d12 = d.createElement("td");
    var d13 = d.createElement("td");
    var d21 = d.createElement("td");
    var d22 = d.createElement("td");
    var d23 = d.createElement("td");
    d11.appendChild(d.createTextNode("1"));
    d12.appendChild(d.createTextNode("2"));
    d13.appendChild(d.createTextNode("3"));
    d21.appendChild(d.createTextNode("4"));
    d22.appendChild(d.createTextNode("5"));
    d23.appendChild(d.createTextNode("6"));
    r1.appendChild(d11);
    r1.appendChild(d12);
    r1.appendChild(d13);
    r2.appendChild(d21);
    r2.appendChild(d22);
    r2.appendChild(d23);
}
</script>

<script type="text/javascript">

function ShowDivInnerHTML()
{
    var c = document.getElementById('container1');
    alert("Created by innerHTML:\n" + c.innerHTML);
    c = document.getElementById('container2');
    alert("Created by create & append:\n" + c.innerHTML);
}

</script>

<title>Dynamic Table in DIV</title>
</head>
<body onload="buildTableWithCreateElement();buildTableWithInnerHTML();">
<h2>Generated by setting innerHTML</h2>
<div id="container1"></div>
<h2>Generated via createElement & appendChild</h2>
<div id="container2"></div>
<input type="button" value="Show it!" onClick="ShowDivInnerHTML();">
<br>
</body>
</html>



In case you should still have some doubts feel free to leave a comment.

Happy coding!

-MRB


var sb = new StringBuilder();
mainDiv.RenderControl(new HtmlTextWriter(new StringWriter(sb)));

string s = sb.ToString();


这篇关于如何获得div的内部html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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