當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。