📜  如何使用 jQuery 设置没有值的属性?

📅  最后修改于: 2022-05-13 01:55:53.951000             🧑  作者: Mango

如何使用 jQuery 设置没有值的属性?

有很多方法可以使用 jQuery 设置没有值的属性。 jQuery prop()方法用于获取或设置属性的值。要更改文档对象模型的选中、默认选中、禁用、selectedIndex、默认选中和选中状态等属性,请使用 jQuery .prop()方法。此外,我们可以使用attr() 方法来做到这一点。下面这两种方法都在示例的帮助下进行了描述。
方法 1:您可以使用 jQuery attr() 方法,该函数用于设置匹配元素集的属性,然后传递一个空字符串。这将相当于设置没有值的属性。

  • 句法 :
.attr(attribute-name, value to be passed)
  • 示例 1:
html


 

    
    
      Setting attribute without value
    
    
    

 

 
     

      Let us learn about       method       for setting attributes.     

        To set the value of attribute, use :     
          


html


 

 
    
    
    

 

    

GeeksforGeeks

           Setting attributes without value using jQuery          
    
                
        This is div.     
    

        This is paragraph.     

      
        This is div.     
   


html


 

    
     
    
        Setting attributes without
        values using jQuery
    
     
    
     
    

 

    

        GeeksforGeeks     

                             

          


  • 输出:

方法 2:这是通过使用 jQuery prop() 方法完成的,该方法用于开发仅名称属性。

  • 句法:
prop(para1, para2)
  • 示例 1:

html



 

 
    
    
    

 

    

GeeksforGeeks

           Setting attributes without value using jQuery          
    
                
        This is div.     
    

        This is paragraph.     

      
        This is div.     
   
  • 输出 :

  • 示例 2:

html



 

    
     
    
        Setting attributes without
        values using jQuery
    
     
    
     
    

 

    

        GeeksforGeeks     

                             

          
  • 输出: