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


jQuery callbacks.has()用法及代码示例


jQuery中的callbacks.has()方法用于回答列表中是否附加了任何回调。如果将回调作为参数传递,则它将回答它是否在列表中。

用法:

callbacks.has([callback])

参数:

  • callback:该参数在列表中定义要搜索的回调。

返回值:此方法返回true或false。

范例1:此示例返回“true”,因为‘func’在列表中。



<!DOCTYPE HTML> 
<html> 
  
<head> 
    <script src= 
"https://code.jquery.com/jquery-3.5.0.js"> 
    </script> 
</head> 
  
<body style="text-align:center;"> 
    <h1 style="color:green;"> 
        GeeksForGeeks 
    </h1> 
  
    <p> 
        JQuery | callbacks.has() method 
    </p> 
      
    <button onclick="Geeks();"> 
        click here 
    </button> 
      
    <p id="GFG"></p> 
  
    <script> 
        var el_down = document.getElementById("GFG"); 
        var res = ""; 
        var callbacks = jQuery.Callbacks(); 
          
        function Geeks() { 
            var func = function (val) { 
                res = res + "value passed is - " + val; 
            }; 
            callbacks.add(func); // function added to list 
            callbacks.fire("gfg_1"); 
            el_down.innerHTML = callbacks.has(); 
        }  
    </script> 
</body> 
  
</html>

输出:

范例2:此示例返回“false”,因为“ func2”不在列表中。

<!DOCTYPE HTML> 
<html> 
  
<head> 
    <script src= 
"https://code.jquery.com/jquery-3.5.0.js"> 
    </script> 
</head> 
  
<body style="text-align:center;"> 
    <h1 style="color:green;"> 
        GeeksForGeeks 
    </h1> 
  
    <p> 
        JQuery | callbacks.has() method 
    </p> 
      
    <button onclick="Geeks();"> 
        click here 
    </button> 
      
    <p id="GFG_DOWN"></p> 
  
    <script> 
        var el_down = document.getElementById("GFG_DOWN"); 
        var res = ""; 
        var callbacks = jQuery.Callbacks(); 
  
        function Geeks() { 
            var func1 = function (val) { 
                res = res + "value passed is - " + val; 
            }; 
            var func2 = function (val) { 
                res = res + "value passed is - " + val; 
            }; 
  
            // Function added to list 
            callbacks.add(func1);  
            callbacks.fire("gfg_1"); 
            el_down.innerHTML = callbacks.has(func2); 
        }  
    </script> 
</body> 
  
</html>

输出:




相关用法


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