2011-10-01 47 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