How do I put a clear button inside my HTML text input box like the iPhone does?

前端 未结 10 1995
太阳男子
太阳男子 2020-12-02 04:21

I want to have a nice little icon that, when clicked will clear the text in the box.

This is to save space rather than having a clear link outside of t

10条回答
  •  有刺的猬
    2020-12-02 05:23

    @thebluefox has summarized the most of all. You're only also forced to use JavaScript to make that button to work anyway. Here's an SSCCE, you can copy'n'paste'n'run it:

    
    
        
            SO question 2803532
            
            
            
        
        
            
        
    
    

    Live example here.

    jQuery is by the way not necessary, it just nicely separates the logic needed for progressive enhancement from the source, you can of course also go ahead with plain HTML/CSS/JS:

    
    
        
            SO question 2803532, with "plain" HTML/CSS/JS
            
        
        
            
                
                
            
        
    
    

    You only ends up with uglier HTML (and non-crossbrowser compatible JS ;) ).

    Note that when the UI look'n'feel isn't your biggest concern, but the functionality is, then just use instead of . It'll show the (browser-specific) clear button on HTML5 capable browsers.

提交回复
热议问题