📌  相关文章
📜  如何查找名称属性包含单词“geeks”并设置背景颜色的所有div?

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

jQuery的包含在其帮助下,我们可以选择具有name属性的元素作为特定的字符串或者选择含有特定字符串或特定字符串或终止与特定字符串开头或不含有特定字符串的元素属性选择一个名称属性。

在本文中,我们将学习使用 jQuery 属性选择器查找名称属性包含单词“geeks”并设置背景颜色为绿色的所有部门。

方法一:为了找到包含特定字符串的 name 属性,我们可以使用带有 ~ 的属性选择器来选择所有包含具有 name 属性的单词的元素作为极客。

句法:

$("[attribute~='word']");

HTML代码:

HTML


  

    
    
    
     
    
    
    

  

    

Geeks for Geeks

            
        jQuery is one of the powerful libraries          of javascript which has many powerful          methods that make the manipulation of          DOM much simpler using the selectors and         makes the interaction with DOM much easier.     
       
        jQuery is one of the powerful libraries of          javascript which has many powerful methods          that make the manipulation of DOM much simpler         using the selectors and makes the interaction          with DOM much easier.     
       
        jQuery is one of the powerful libraries of          javascript which has many powerful methods          that make the manipulation of DOM much simpler         using the selectors and makes the interaction          with DOM much easier.   
          


HTML


  

    
    
    
     
    
    
  
    

  

    

Geeks for Geeks

            
        jQuery is one of the powerful libraries of          javascript which has many powerful methods          that make the manipulation of DOM much simpler         using the selectors and makes the interaction          with DOM much easier.     
       
        jQuery is one of the powerful libraries of          javascript which has many powerful methods          that make the manipulation of DOM much simpler         using the selectors and makes the interaction          with DOM much easier.     
       
        jQuery is one of the powerful libraries of          javascript which has many powerful methods          that make the manipulation of DOM much simpler         using the selectors and makes the interaction          with DOM much easier.     
          


输出:

方法二:使用*选择所有包含“geeks”的div ,但~和*的区别在于~选择用空格隔开的单词。对于 * 不需要任何空格,它甚至可以选择一个子字符串。

句法:

$("[attribute*='word']");

HTML代码:

HTML



  

    
    
    
     
    
    
  
    

  

    

Geeks for Geeks

            
        jQuery is one of the powerful libraries of          javascript which has many powerful methods          that make the manipulation of DOM much simpler         using the selectors and makes the interaction          with DOM much easier.     
       
        jQuery is one of the powerful libraries of          javascript which has many powerful methods          that make the manipulation of DOM much simpler         using the selectors and makes the interaction          with DOM much easier.     
       
        jQuery is one of the powerful libraries of          javascript which has many powerful methods          that make the manipulation of DOM much simpler         using the selectors and makes the interaction          with DOM much easier.     
          

输出: