将元素添加到网络浏览器
本文关键字:网络 浏览器 添加 元素 | 更新日期: 2023-09-27 18:30:26
我在我的网络浏览器webbSPPagina中加载了一个id="table1"的表。我需要添加额外的行。使用此代码,我可以将行添加到表元素中,但它不会显示在 Web 浏览器上。我需要在我的网络浏览器上更改某些内容吗?
HtmlElement element = webbSPPagina.Document.GetElementById("table1");
HtmlElement mTBody = element.FirstChild;
HtmlElement mTR = webbSPPagina.Document.CreateElement("TR");
HtmlElement mTD1 = webbSPPagina.Document.CreateElement("TD");
HtmlElement mTD2 = webbSPPagina.Document.CreateElement("TD");
HtmlElement mTD3 = webbSPPagina.Document.CreateElement("TD");
mTD1.Style = "VERTICAL-ALIGN: top";
mTD2.Style = "VERTICAL-ALIGN: top";
mTD3.Style = "VERTICAL-ALIGN: top";
mTD1.SetAttribute("class", "ms-rtetablecells");
mTD2.SetAttribute("class", "ms-rtetablecells");
mTD3.SetAttribute("class", "ms-rtetablecells");
mTD1.InnerText = "Teamviewer Id";
mTD2.SetAttribute("id", "TeamviewerId");
mTD3.SetAttribute("id", "TeamviewerIdExtra");
mTR.AppendChild(mTD1);
mTR.AppendChild(mTD2);
mTR.AppendChild(mTD3);
mTBody.AppendChild(mTR);
我尝试了您的代码,如果我按以下方式执行此操作,它对我有用
//Add table in the form load event
private void Form1_Load(object sender, EventArgs e)
{
webbSPPagina.DocumentText = "<table id='table1'><tr><td>hello</td></tr></table>";
}
//Added your code in a button click event
private void button1_Click(object sender, EventArgs e)
{
HtmlElement element = webbSPPagina.Document.GetElementById("table1");
HtmlElement mTBody = element.FirstChild;
HtmlElement mTR = webbSPPagina.Document.CreateElement("tr");
HtmlElement mTD1 = webbSPPagina.Document.CreateElement("td");
HtmlElement mTD2 = webbSPPagina.Document.CreateElement("td");
HtmlElement mTD3 = webbSPPagina.Document.CreateElement("td");
mTD1.Style = "VERTICAL-ALIGN: top";
mTD2.Style = "VERTICAL-ALIGN: top";
mTD3.Style = "VERTICAL-ALIGN: top";
mTD1.SetAttribute("class", "ms-rtetablecells");
mTD2.SetAttribute("class", "ms-rtetablecells");
mTD3.SetAttribute("class", "ms-rtetablecells");
mTD1.InnerText = "Teamviewer Id";
mTD2.SetAttribute("id", "TeamviewerId");
mTD3.SetAttribute("id", "TeamviewerIdExtra");
mTR.AppendChild(mTD1);
mTR.AppendChild(mTD2);
mTR.AppendChild(mTD3);
mTBody.AppendChild(mTR);
}