📜  如何在没有 jQuery 中的 addClass() 方法的情况下向元素添加类?

📅  最后修改于: 2021-09-01 02:55:13             🧑  作者: Mango

任务是在 jQuery 的帮助下,在不使用 addClass() 方法的情况下向元素添加一个新类。下面讨论两种方法:

方法一:为了执行操作,我们可以使用toggleClass()方法来切换我们想要添加到元素的类。

  • 例子:
    
    
      
    
        
            How to add class to an element without
            addClass() method in jQuery ?
        
      
        
      
        
    
      
    
      
        

            GeeksForGeeks     

           

           

    This is the element

                   

              
  • 输出:

方法二:我们也可以使用attr()方法prop()方法给元素添加一个新的属性’class’。

  • 例子:
    
    
      
    
        
            How to add class to an element without
            addClass() method in jQuery ?
        
      
        
      
        
    
      
    
      
        

            GeeksForGeeks     

               

           

    This is the element

                           

              
  • 输出: