为什么在 TWebBrowser 中从未调用 ommouseenter 事件(已安装 IE9)

发布于 2024-11-26 21:53:52 字数 319 浏览 2 评论 0原文

我想处理 ommouseenter 事件,但事件从未从 TWebBrowser 调用。虽然我成功捕获了 onmousemove 事件。当前浏览器IE9。我的代码:

var
  D3: IHTMLDocument3;
begin

  if Supports(WebBrowser1.Document, IHTMLDocument3, D3) then
  begin
    eo1 := TEventObject.Create(self.EventHadler);
    D3.attachEvent('onmouseenter', eo1);
  end;
end;

I want to handle ommouseenter event, but event never called from TWebBrowser. Although I successfully catch onmousemove event. Current Browser IE9. My code:

var
  D3: IHTMLDocument3;
begin

  if Supports(WebBrowser1.Document, IHTMLDocument3, D3) then
  begin
    eo1 := TEventObject.Create(self.EventHadler);
    D3.attachEvent('onmouseenter', eo1);
  end;
end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-12-03 21:53:52

I quess that the reason is that ommouseenter event does not bubble. IOW it only fires when the mouse pointer moves over the element you attached the event to. So you can't use one "generic" eventhandler, you have to attach to each and every element youre instred in.

蓝天白云 2024-12-03 21:53:52

您必须将事件处理程序附加到每个感兴趣的元素。如果您对所有元素感兴趣,那么您必须循环遍历所有元素:

var
  All: IHTMLElementCollection;
  Element2: IHTMLElement2;
  i: Integer;
begin
  Handler:= TEventObject.Create(Self.EventHandler);
  All:=(WebBrowser1.ControlInterface.Document as IHTMLDocument2).All;
  for i:=0 to All.Length-1 do
  begin
    Element2:=All.item(i,EmptyParam) as IHTMLElement2;
    Element2.AttachEvent('onmouseenter', Handler);
  end;
end;

因此,在理想的情况下,您将在收到感兴趣文档的 DocumentComplete 后附加处理程序,并在 BeforeNavigate< 中再次分离/代码>。

但您应该注意,可能存在几个问题:

  • 文档可能永远无法完成加载(这通常是由于某些广告服务器不提供服务时包含广告的框架引起的,因此该框架会阻止主文档触发 DocumentComplete),因此您的事件处理程序永远不会被附加,
  • 脚本可能会修改页面并添加元素,然后这些元素不会附加事件处理程序
  • ,您必须手动进入 FRAMEs/ IFRAMEs 并附上 我也

希望它能像你的方法一样简单。这样我们就可以省去很多麻烦。

You have to attach the event handler to every element of interest. If you are interested in all elements then you have to loop through all elements:

var
  All: IHTMLElementCollection;
  Element2: IHTMLElement2;
  i: Integer;
begin
  Handler:= TEventObject.Create(Self.EventHandler);
  All:=(WebBrowser1.ControlInterface.Document as IHTMLDocument2).All;
  for i:=0 to All.Length-1 do
  begin
    Element2:=All.item(i,EmptyParam) as IHTMLElement2;
    Element2.AttachEvent('onmouseenter', Handler);
  end;
end;

So in an ideal world you would attach the handler after receiving DocumentComplete for the document of interest and detach again in BeforeNavigate.

There can be several problems though you should be aware of:

  • the document may never finish loading (this is often caused by frames containing advertisements when some ad server doesn't serve, so the frame prevents the main document from firing the DocumentComplete) thus your event handlers would never be attached
  • scripts might modify the page and add elements which then wouldn't have the event handler attached
  • you manually have to go into FRAMEs/ IFRAMEs and attach the handlers there

I also wish it would be as easy as your approach. This would save us a lot of this hassle.

握住你手 2024-12-03 21:53:52

正如 ain 所指出的,onmouseenter 不会冒泡,而是作为 MSDN 说,

与 onmouseover 事件不同,onmouseenter 事件不会冒泡。换句话说,当用户将鼠标指针移到对象包含的元素上时,onmouseenter 事件不会触发,而 onmouseover 事件会触发。

因此,您可以使用 onmouseover

当用户将鼠标指针移入对象时,该事件发生,除非用户将鼠标指针移出对象然后再移回对象,否则该事件不会重复。

procedure MyEvent;
var
  Doc: OleVariant;
begin
  Doc := Form1.WebBrowser1.Document;
  Form1.Label1.Caption := Doc.parentWindow.event.srcElement.outerHTML;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  D3: IHTMLDocument3;
begin
  if Supports(WebBrowser1.Document, IHTMLDocument3, D3) then
    D3.attachEvent('onmouseover', TEventObject.Create(MyEvent) as IDispatch);
end;

As ain has pointed out, onmouseenter does not bubble, but as MSDN says,

Unlike the onmouseover event, the onmouseenter event does not bubble. In other words, the onmouseenter event does not fire when the user moves the mouse pointer over elements contained by the object, whereas onmouseover does fire.

So you can use onmouseover:

The event occurs when the user moves the mouse pointer into the object, and it does not repeat unless the user moves the mouse pointer out of the object and then back into it.

procedure MyEvent;
var
  Doc: OleVariant;
begin
  Doc := Form1.WebBrowser1.Document;
  Form1.Label1.Caption := Doc.parentWindow.event.srcElement.outerHTML;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  D3: IHTMLDocument3;
begin
  if Supports(WebBrowser1.Document, IHTMLDocument3, D3) then
    D3.attachEvent('onmouseover', TEventObject.Create(MyEvent) as IDispatch);
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文