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

get iframe id according to position of user cursor

本文关键字:获取 iframe ID 位置 用户 光标      更新时间:2023-09-26

我的网页中有 2 个带有内容可编辑(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);  
      });
   }); 
});

但它不起作用

有什么建议吗?

可以使用mouseenter:

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

这里有一个小提琴:http://jsfiddle.net/saq02L9o/

使用悬停
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>