📜  如何在运行时设置 href 属性?

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

我们知道如何设置锚标签 int HTML的 href 属性,但有时我们可能需要在运行时设置 href 属性,例如,当用户向我们提供一个 url 并且我们想在运行时设置它时。我们可以在 jQuery的帮助下做到这一点。

示例 1:在此示例中,使用 jquery ,当用户单击 set href 按钮时,我们将 attr()方法设置为用户在 input 标签中输入的 url。

html


    
        
        
        
        
        Set href attribute at runtime
        
    
    
        
            

GeeksforGeeks

            Set href attribute at runtime             
                                               
           


html


    
        
        
        
        
        Set href attribute at runtime
        
    
    
        
            

GeeksforGeeks

            Set href attribute at runtime             
                     
           


输出:

示例 2:在此示例中,我们将 div ‘link’ 中的锚标记替换为另一个锚标记以更改href值。这是我们可以更改href属性值的另一种方式。

html



    
        
        
        
        
        Set href attribute at runtime
        
    
    
        
            

GeeksforGeeks

            Set href attribute at runtime             
                     
           

输出: