根据用户光标的位置获取iframe id

get iframe id according to position of user cursor

我的网页中有 2 个带有 contenteditable(true) 的 iframe, 我的问题是如何根据光标插入符号的位置获取 iframe id?

例如:iframe1 和 iframe2,如果用户光标插入符号位于 iframe2 中,那么它会提醒 id frame2

我已经尝试过以下代码

  $(document).ready(function(){

   $('iframe').on('change mouseup mousedown mouseout keydown', function(){

      var iframeID = $(this).attr('id');

      $(this).contents().unbind(); 

      $(this).contents().bind('click', function(){

          alert(iframeID);  
      });
   }); 

});

但是没用

有什么建议吗?

使用悬停
http://api.jquery.com/hover/

$("iframe").hover(
  function() {
    alert('mouse in to ' + $(this).attr('id'))
  }, function() {
    //alert('mouse out of '+$(this).attr('id'))
  }
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<iframe id="foo">foo</iframe>
<iframe id="bar">bar</iframe>

可以使用鼠标输入:

$(document).ready(function() {
    $('iframe').mouseenter(function(h) {
        var id = $(this).attr('id');
        alert(id);
    });
});

这里是fiddle:http://jsfiddle.net/saq02L9o/