当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


jQuery event.relatedTarget用法及代码示例


jQuery event.relatedTarget是一个内置属性,用于查找鼠标移动时正在进入或退出的元素。

用法:

event.relatedTarget

参数:它不接受任何参数,因为它是属性而不是函数。

示例 1:此示例显示event.relatedTarget 属性的工作原理。

HTML


<!DOCTYPE html> 
<html> 
  
<head> 
    <script src= 
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
    </script> 
    <!--jQuery code to show working of this property-->
    <script> 
            $(document).ready(function () { 
                $("div, p").mouseenter(function (event) { 
                    $("#d2").html("Pointer at : "  
                        + event.relatedTarget.nodeName); 
                }); 
            }); 
    </script> 
    <style> 
        #d1 { 
            height: 100px; 
            width: 50%; 
            padding: 10px; 
            border: 2px solid green; 
        } 
  
        #d2 { 
            height: 20px; 
            width: 50%; 
            padding: 10px; 
            margin-top: 10px; 
            border: 2px solid green; 
        } 
    </style> 
</head> 
  
<body> 
    <!-- this is outer div element -->
    <div id="d1"> 
        <!-- this is inner div element -->
        <div>This is a div element </div> 
        <!-- this is paragraph element -->
        <p style="background-color:lightgreen"> 
            This is a paragraph 
        </p> 
    </div> 
    <div id="d2"></div> 
</body> 
  
</html>

输出:

示例 2:在此示例中,当鼠标从元素退出时,将显示一个弹出窗口。

HTML


<!DOCTYPE html> 
<html> 
  
<head> 
    <script src= 
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
    </script> 
    <!--jQuery code to show working of this property-->
    <script> 
            $(document).ready(function () { 
                $("div, p").mouseenter(function (event) { 
                    $(this).animate({ fontSize: "+=14px"}); 
                }); 
                $("div, p").mouseleave(function (event) { 
                    $(this).animate({ fontSize: "-=14px"}); 
                    alert("Pointer at : "  
                        + event.relatedTarget.nodeName); 
                }); 
            }); 
    </script> 
    <style> 
        #d1, #d2 { 
            background-color: lightgreen; 
            height: 30px; 
            width: 50%; 
            padding: 10px; 
            margin-top: 10px; 
            border: 2px solid green; 
        } 
    </style> 
</head> 
  
<body> 
    <div id="d1"> 
        Geeksforgeeks 
    </div> 
    <p id="d2"> 
        A computer science portal 
    </p> 
</body> 
  
</html>

输出:



相关用法


注:本文由纯净天空筛选整理自kundankumarjha大神的英文原创作品 jQuery event.relatedTarget Property。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。