2011-10-01 41 views
0

Webbrowser控件中有一个网页(文档)。我知道如何将任何HTML元素插入到正文中。在Webbrowser的文档中插入HTML对象元素

问题是,我该如何插入<object>及其<param>?我必须保留现有的文件,所以我不会使用Webbrowser1.Document.Write()

Dim player As HtmlElement = Webbrowser1.Document.CreateElement("object") 
player.SetAttribute("width", "640") 
player.SetAttribute("height", "480") 

Dim param As HtmlElement = Webbrowser1.Document.CreateElement("param") 
param.SetAttribute("name", "movie") 
param.SetAttribute("value", "http://foo.bar.com/player.swf") 

player.AppendChild(param) '<== throws an exception 

Webbrowser1.Document.Body.AppendChild(player) 

抛出的异常:“值不是一组有效的查找值中”(我猜,因为我使用VS2010的法语版本,这给了我“香格里拉valeur n'est PAS包括丹斯拉谱斑attendue。”

替代方法

终于可以追加使用Navigate()方法使用Javascript URI的<object>元件。这有点难看,但它有效。

回答

0

嗯,我意识到我的答案正是你在做什么。哎呀。

MSDN文档显示了很多object Object的方法,但它没有指定appendChild。方法文档指定它是一组对象的一部分,但不是对象。 applyElement看起来可能会起作用。

此外,insertAdjacentHTML方法和innerHTML参数在对象对象上有效,并且可能有所帮助。


Hrm - 我对.net不熟悉,但是我在Javascript中做了类似的事情。 Chrome浏览器似乎产生正确的文档设置为以下:

<script> 
    var newObj=document.createElement("object"); 
    newObj.setAttribute('width', '640'); 
    newObj.setAttribute('height', '480'); 
    newObj.setAttribute('data', 'http://ozdoctorwebsite.com/test-4938161.gif'); 

    var newParam=document.createElement("param"); 
    newParam.setAttribute('name', 'test'); 

    newObj.appendChild(newParam); 
    document.body.appendChild(newObj); 
</script> 

基本上 - 你param元素添加到播放器和附加玩家的document.body的。

+0

抛出的异常堆栈跟踪包含'InsertAdjacentElement'方法,所以我认为它在内部使用。无论如何,直接使用它会引发相同的异常。我没有在.NET 4中找到'ApplyElement'方法,'InnerHTML'属性抛出另一个异常。 – Velcro