📜  如何查找在 jQuery 中禁用的所有元素?

📅  最后修改于: 2021-11-24 04:51:46             🧑  作者: Mango

在本文中,我们将学习如何查找页面上所有使用 jQuery 禁用的元素。

方法: :disabled选择器可用于获取页面上当前禁用的所有元素。这些元素使用each()方法进行迭代。然后可以使用 each() 方法的函数内的this引用单独访问元素。

可以通过在禁用选择器前面指定元素类型来选择特定类型的元素,否则检查页面上的所有元素是否禁用。例如,使用“input:disabled”只会选择输入类型的禁用元素。

句法:

$(".btn").click(function () {
    
    let disabled_elems = "";
  
    // Get all the disabled elements
    $(":disabled").each(function (index) {
    
        // Add a border to the elements
        $(this).css("border", "10px red dashed");

        // Add the IDs to the list
        disabled_elems += "
  • " + ($(this).attr("id")) + "
  • "; }); $(".elems").html(disabled_elems); });

    以下示例说明了上述方法:

    例子:

    HTML
    
    
      
    
        
    
      
    
        

            GeeksforGeeks     

                    How to find all elements that         are disabled in jQuery?                  

    Inputs

                

                

    Buttons

                

                   

    Selects

                

                
           

            Click on the button to mark all         the disabled elements and         get their element ID     

                

           The ids of the disabled elements are:     
                


      输出: