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

前端 未结 10 2038
太阳男子
太阳男子 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:17

    @Mahmoud Ali Kaseem

    I have just changed some CSS to make it look different and added focus();

    https://jsfiddle.net/xn9eogmx/81/

    $('#clear').click(function() {
      $('#input-outer input').val('');
      $('#input-outer input').focus();
    });
    body {
      font-family: "Arial";
      font-size: 14px;
    }
    #input-outer {
      height: 2em;
      width: 15em;
      border: 1px #777 solid;
      position: relative;
      padding: 0px;
      border-radius: 4px;
    }
    #input-outer input {
      height: 100%;
      width: 100%;
      border: 0px;
      outline: none;
      margin: 0 0 0 0px;
      color: #666;
      box-sizing: border-box;
      padding: 5px;
      padding-right: 35px;
      border-radius: 4px;
    }
    #clear {
      position: absolute;
      float: right;
      height: 2em;
      width: 2em;
      top: 0px;
      right: 0px;
      background: #aaa;
      color: white;
      text-align: center;
      cursor: pointer;
      border-radius: 0px 4px 4px 0px;
    }
    #clear:after {
      content: "\274c";
      position: absolute;
      top: 4px;
      right: 7px;
    }
    #clear:hover,
    #clear:focus {
      background: #888;
    }
    #clear:active {
      background: #666;
    }
    
    

提交回复
热议问题