js模拟h5的placeholder

每天都在受兼容性折磨

js模拟h5的placeholder

html

 <input class="sortSearch_input" id="sortSearch_input" type="text" placeholder="请输入您要搜索的产品名称、品牌或型号" />

js

var funPlaceholder = function(element) {
    var placeholder = '';
    if (element && !("placeholder" in document.createElement("input")) && (placeholder = element.getAttribute("placeholder"))) {
        element.onfocus = function() {
           if (this.value === placeholder) {
               this.value = "";
        }
        this.style.color = '';
    };
    element.onblur = function() {
       if (this.value === "") {
           this.value = placeholder;
           this.style.color = 'graytext';
        }
    };

    //样式初始化
    if (element.value === "") {
        element.value = placeholder;
        element.style.color = 'graytext';
     }
  }
};

funPlaceholder(document.getElementById("sortSearch_input"));

猜你喜欢

转载自blog.csdn.net/caimaomaocai/article/details/82984292
今日推荐