input 内置属性 样式更改 总结

目录

1. input placeholder 提示文字 样式更改

2. input radio 单选框 样式更改

3. input checkbox 复选框 样式更改

4. input type=number 调控数字的按钮 样式更改


1. input placeholder 提示文字 样式更改

  • WebKit / Blink / Edge —— ::-webkit-input-placeholder { color: #fff; }
  • IE(10/11)—— :-ms-input-placeholder { color: #fff; }
  • Firefox(4-18) —— :-moz-placeholder { color: #fff; }
  • Firefox(19+) —— ::-moz-placeholder { color: #fff; }

  • 注意:更改 IE 中 placeholder 的样式,需要引入兼容性代码
<script>
  $(function () {
    // 判断浏览器是否支持 placeholder 不支持则执行自定义代码
    if (!placeholderSupport()) {
      $("[placeholder]")
        .focus(function () {
          var input = $(this);
          // 获取焦点之后,type 属性修改为密码框,如果没有密码框,此处判断可以删除
          if (input[0].id == "userPassword") {
            input.prop("type", "password");
          }
          if (input.val() == input.attr("placeholder")) {
            input.val("");
            input.removeClass("placeholder");
          }
        })
        .blur(function () {
          var input = $(this);
          if (input.val() == "" || input.val() == input.attr("placeholder")) {
            // 将所有 input 置为文本框,主要为了重置密码框
            input.prop("type", "text");
            input.addClass("placeholder");
            input.val(input.attr("placeholder"));
          } else {
            // 密码框填写密码后密码后恢复密码框
            if (input[0].id == "userPassword") {
              input.prop("type", "password");
            }
          }
        })
        .blur();
    }
  });
  // 判断 placeholder 属性是否可以使用的方法
  function placeholderSupport() {
    return "placeholder" in document.createElement("input");
  }
</script>

2. input radio 单选框 样式更改

<label> 选中单选框 </label>
<input type="radio" checked name="danxuan" value="选中单选框" />

<label> 未选中单选框 </label>
<input type="radio" name="danxuan" value="未选中单选框" />

<style>
  /* 隐藏原有的 radio */
  input[type="radio"] {
    position: absolute;
    clip: rect(0, 0, 0, 0);
  }
  input[type="radio"] + label::before {
    /* 不换行空格 */
    content: "\a0";
    display: inline-block;
    vertical-align: middle;
    font-size: 18px;
    width: 14px;
    height: 14px;
    margin-right: 2px;
    margin-top: -4px;
    border-radius: 50%;
    -webkit-border-radius: 50%;
    -moz-border-radius: 50%;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    border: 1px solid #c6cbd0;
  }
  /* 添加颜色 */
  input[type="radio"]:checked + label::before {
    background-color: #28a7ff;
    border: 1px solid #28a7ff;
    background-clip: content-box;
    padding: 2px;
  }
</style>

3. input checkbox 复选框 样式更改

<label> 选中复选框 </label>
<input type="checkbox" checked name="复选框选中" />

<label> 未选中复选框 </label>
<input type="checkbox" name="复选框未选中" />

<style>
  input[type="checkbox"] {
    width: 12px;
    height: 12px;
    box-sizing: border-box;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    -ms-box-sizing: border-box;
    border-radius: 2px;
    background: #e2e2e2;
    margin-right: 2px;
    outline: none;
    -webkit-appearance: none;
  }
  /* 定义复选框选中的样式 */
  input[type="checkbox"]:checked {
    background: #028dfb no-repeat center;
  }
</style>

4. input type=number 调控数字的按钮 样式更改

  • 默认情况下,是有上图右侧的那种箭头按钮的,去除它的方法如下:
<input type="number">
 
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    -webkit-appearance: none;
}
			 
input[type="number"] {
    -moz-appearance: textfield;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lyrelion

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值