将鼠标悬停在位于我的页面上的 iframe 上的绑定事件,其中包含包含的 src

Binding event with mouseover on an iframe that sits on my page with a src that contains

本文关键字:事件 iframe 绑定 src 包含包 悬停 鼠标 于我的 我的      更新时间:2023-09-26

我有一些第三方iframe位于我的页面上,我想捕获用户何时mousesover它们,只有当这些iframe的src在src文本中的任何位置包含Twitter或facebook时。由于我无法捕获点击(我可以使用他们的自定义方法,因此我想将代码完全分开)。我想捕获用户何时将鼠标鼠标进入iframe(位于我的页面上,不是由他们带入的)。iframe 的"SRC"在我的网站之外,但实际调用在我的页面中。有什么想法吗?

翼:

var _self = this;
$("iframe src").filter('/facebook|twitter/')
    .mouseover(function(event){
    event.stopPropagation();
    _self.somemethodcall();
});

或类似的东西:

var _self = this;
$('iframe').filter(function() {
    return $(this).attr('src').match(/facebook|twitter/);
}).bind("mouseover", function(event) {
    event.stopPropagation();
    _self.somemethodcall();
});

它看起来像这样:

var a = /facebook|twitter/;
$('iframe').live('mouseover', 
  function () {
      if(a.test($(this).attr('src'))){
             alert('Found it!');
      }
  }  
);​

首先,我们将正则表达式放入名为 a 的变量中。然后,在悬停 iframe 时,我们测试正则表达式的iframe src,如果 src 的值包含 facebooktwitter,则返回 true。

一个工作示例:http://jsfiddle.net/2PZ2c/(第三个是 google.com 来源的那个)

可读性:

http://api.jquery.com/hover/

http://www.w3schools.com/jsref/jsref_regexp_test.asp

旁注:

该解决方案也适用于其他处理程序,如mouseentermouseleave等。

编辑

我已经编辑了我的代码以改用 .live() 函数。此函数会将事件处理程序附加到两个当前元素作为未来元素。

示例:http://jsfiddle.net/2PZ2c/1/

来源:http://api.jquery.com/live/