javascript占位符

摘要:
传统javascript:Thetextisinsertedinthevalueattribute.Onfocus,itchecksifthevalueis"search"andreturnsemptytoclearthefield.Ifthevalueisempty,itreturns"search."Asyoucansee,thiswayisnotanefficientwaybecauseea

传统javascript:

The text is inserted in the value attribute. On focus, it checks if the value is "search" and returns empty to clear the field. If the value is empty, it returns "search." As you can see, this way is not an efficient way because each field has to be checked.

<input type="text" value="Search"onfocus="if (this.value == 'Search') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search';}">

Now with HTML5 placeholder, it is more semantic to use placeholder than value attribute. However, placeholder text is not supported by all browsers. To make it cross-browser,ModernizrandjQuerycome in handy here.

Modernizr is used here to check if placeholder is supported. If placeholder is not supported, the jQuery code will run. It finds all elements with placeholder attribute and stores in a variable. It then compares the input value with the placeholder attribute. If the input value is empty, it will display the placeholder text and add a "placeholder" class to the input field. ViewDemo.

To use this on your site, download a copy of Modernizr and jQuery and paste the following code any where in your html page (be sure the jquery.js and modernizr.js file is in correct path).

<script src="http://t.zoukankan.com/jquery.js"></script>
<script src="http://t.zoukankan.com/modernizr.js"></script>

<script>$(document).ready(function(){

if(!Modernizr.input.placeholder){

    $('[placeholder]').focus(function() {
      var input = $(this);
      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.addClass('placeholder');
        input.val(input.attr('placeholder'));
      }
    }).blur();
    $('[placeholder]').parents('form').submit(function() {
      $(this).find('[placeholder]').each(function() {
        var input = $(this);
        if (input.val() == input.attr('placeholder')) {
          input.val('');
        }
      })
    });

}

</script>

via:http://webdesignerwall.com/tutorials/cross-browser-html5-placeholder-text

另外一个原生的javascript实现;

functionadd_placeholder (id, placeholder)
{
    var el =document.getElementById(id);
    el.placeholder =placeholder;
 
    el.onfocus = function()
    {
        if(this.value == this.placeholder)
        {
            this.value = '';
            el.style.cssText  = '';
        }
    };
 
    el.onblur = function()
    {
        if(this.value.length == 0)
        {
            this.value = this.placeholder;
            el.style.cssText = 'color:#A9A9A9;';
        }
    };
 
    el.onblur();
}
 
//Add right before </body> or inside a DOMReady wrapper
add_placeholder('myInputField', 'IE Placeholder Text');

<inputtype="text"name="myInputField"value=""placeholder="HTML5 Placeholder Text"id="myInputField">

参考:https://gist.github.com/jaywilliams/1105055#file-example-html

免责声明:文章转载自《javascript占位符》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇CentOS7 升级 openssh 到 openssh-8.0p1版本win32api win32gui win32con 窗口句柄 发送消息 常用方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

TP5.1 爬虫

环境 PHP >= 7.1 !!!PHP >= 7.1 !!!PHP >= 7.1 !!! 安装 composer require jaeger/querylist 后端 //爬虫 public functioncrawler() { if(request()->post()){...

【前端开发】vue项目日程表12月平铺(日历)vue-material-year-calendar插件的使用教程

前言 项目中需要一个12个月平铺能按年份切换的日历功能,找了好多没找到好用的,最后找了个相似的改了下,效果如下  步骤 // 安装依赖 yarn add vue-material-year-calendary 源码 <template> <div class="weekdays-pages"> <div clas...

Feature.js-轻量级浏览器特性检测JavaScript库插件

简要教程 Feature.js是一款轻量级的浏览器特性检测JavaScript库插件。该插件运行速度快,使用简单,文件只有1kb大小。通过Feature.js你可以检测客户浏览器是否支持某些特性,并针对这些特性编写代码。 Feature.js会自动在页面初始化时进行加载,但是它不会自动进行特性检测,直到你在代码中调用它时才会进行指定特性的检测。 通过Fea...

input type date 解决移动端显示placeholder

在最近的一个项目中使用到了html5的一个新标签属性,type="date"时,发现placeholder属性失效无法使用。 如果是这样的效果,那么客户体验是可想而知的差了。 最后想了一下,想到用css+js双保险来搞定它。 方法1: css: input[type="date"]:before{ content:attr(placeholder)...

react hook+antd实现点击发送验证码功能

要实现的效果如图,点击发送验证码,文字变为60秒后重新发送,并且开始倒计时 这是写成一个组件格式component -> eidtPass.tsx import React, { useState } from 'react'import { Modal, Form, Input, notification } from 'antd'import...

WebUploader.js 上传的几种情况

最近的项目中用到webUploader的地方比较多,总结了几种图片上传的情况。 必要的引用 <link href="http://t.zoukankan.com/~/Scripts/webuploader-0.1.5/webuploader.css" rel="stylesheet" /> <script src="http://t.z...