在 Web 浏览器的文档中插入 HTML 对象元素
在 Webbrowser
控件中,有一个网页(文档)。我知道如何将任何 HTML 元素插入正文中。
问题是,如何插入
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,它给了我“La valeur n'est pas include dans la plage attendue.")
解决方法
最后我可以使用附加一个
In a Webbrowser
control there is a webpage (the document). I know how to insert any HTML element into the body.
The problem is, how can I insert an <object>
along with its <param>
? I must keep the existing document, so I wouldn't use 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)
Thrown exception: "Value is not among the set of valid lookup values" (I guess, because I'm using french version of VS2010, which gives me "La valeur n'est pas comprise dans la plage attendue.")
WORKAROUND
Finally I could append an <object>
element using Navigate()
method with Javascript URI. It's a bit ugly, but it works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我意识到我的答案正是你所做的。哎呀。
MSDN 文档显示了 对象的许多方法Object,但未指定
appendChild
。appendChild
方法文档指定它是一堆对象的一部分,但不是对象。applyElement
看起来可能有用。另外,
insertAdjacentHTML
方法和innerHTML
参数对对象对象有效,并且可能会有所帮助。Hrm - 我不熟悉.net,但我在 Javascript 中做了类似的事情。 Chrome 似乎使用以下内容生成正确的文档设置:基本上 - 将 param 元素附加到播放器并将播放器附加到 document.body。Well, I realized my answer was exactly what you were doing. Oops.
The MSDN documentation shows a lot of methods for the object Object, but it doesn't specify
appendChild
. TheappendChild
method documentation specifies that it is a part of a bunch of objects, but not object.applyElement
looks like it might work.Also, the
insertAdjacentHTML
method andinnerHTML
parameter are valid on object Objects, and may be helpful.Hrm - I'm not familiar with .net, but I have done something similar in Javascript. Chrome seems to produce the correct document setup with the following:Basically - append your param element to the player and append the player to the document.body.