我已将以下内容添加到表单 Load 上的 WebBrowser 控件中:
I have added the following content to a WebBrowser control on form Load:
private void Form1_Load(object sender, EventArgs e) { addFirst(); } private void addfirst() { string html = "<!DOCTYPE html>"; html += "<html><head><title>NewPage</title>"; html += "<style>"; html += "body {font-family: Verdana, Tahoma, sans-serif; font-size: 12px;} .vtop {vertical-align: top;} table th:nth-child(1) {width: 20%;}"; html += "table th:nth-child(2) {width: 30%;} table th:nth-child(3) {width: 50%;}"; html += "</style>"; html += "</head>"; html += "<body>"; html += "<table border='0' id='tableresult'>"; html += "</table>"; html += "</body></html>"; webbrowse.DocumentText = html; }然后在按钮的 Click 事件处理程序上创建一个新的 tr 和两个 td 元素:
Then on Click event handler of a button I want to create a new tr and two td elements:
var doc = webbrowse.Document; var elem = doc.GetElementById("tableresult"); var email = elem.DomElement; var tr = webbrowse.Document.CreateElement("tr"); var td1 = webbrowse.Document.CreateElement("td"); var td2 = webbrowse.Document.CreateElement("td"); tr.AppendChild(td1); tr.AppendChild(td2); elem.AppendChild(tr);但是当我在 WeBrowser 控件中检查源代码时,看不到新的TR或TD.
But when I check the source in the WeBrowser control, I don't see the new TR or the TDs.
我该如何解决?
推荐答案有点棘手,但是您已经快到了.
It's a bit tricky, but you are almost there.
无论是否显式创建了 tbody ,您都需要找到 table 的 tbody 元素并将其添加到该行.
You need to find tbody element of the table and append the row to that, no matter you have created tbody explicitly or not.
调试技巧::右键单击 WebBrowser 控件并选择 View Source ,它会显示初始的 DocumentText ,而不是修改的.如果要查看更新的文档文本,请观看 webBrowser1.Document.Body.InnerHtml .
Debug tip: When you right click on WebBrowser control and choose View Source, it shows the initial DocumentText, not the modified one. If you want to see the updated document text, watch webBrowser1.Document.Body.InnerHtml.
注意:根据文档,设置 DocumentText 就足够了;设置 DocumentText 时,它将导航到 about:blank ,然后根据您设置的文本加载文档内容.因此,发生了所有预期的事情,例如创建Document对象或引发导航",导航"或 DocumentComplete 事件.遵循源代码也显示相同的内容.
Note: According to the documentation, setting DocumentText should be enough; when you set DocumentText, it navigates to about:blank and then loads the document content based on the text which you have set. So all the expected things like creating Document object or raising Navigating, Navigated or DocumentComplete events happen. Following the source code also shows the same.
示例
private void Form1_Load(object sender, EventArgs e) { var html = @" <!DOCTYPE html> <html> <head><title>NewPage</title></head> <body> <table id=""tableresult""> <thead> <th>Column 1</th> <th>Column 2</th> </thead> </table> </body> </html>"; webBrowser1.DocumentText = html; } private void button1_Click(object sender, EventArgs e) { var tr = webBrowser1.Document.CreateElement("tr"); var td1 = webBrowser1.Document.CreateElement("td"); td1.InnerHtml = "value 1"; var td2 = webBrowser1.Document.CreateElement("td"); td2.InnerHtml = "value 2"; tr.AppendChild(td1); tr.AppendChild(td2); webBrowser1.Document.GetElementById("tableresult") .GetElementsByTagName("tbody")[0].AppendChild(tr); }更多推荐
如何在WebBrowser控件的表中插入新行
发布评论