js模拟placeholder效果1

由于有的浏览器不支持placeholder,所以写了一个jquery插件来模拟placeholder效果。

使用方法:$("#IDName").placeholder();

话不多说,直接上代码~~~

(function ($) {
    $.fn.placeholder = function (options) {
        var defaults = {
            pColor: "#BEBEBE",
            pActive: "#999",
            pFont: "14px",
            activeBorder: "#080",
            posL: 8,
            zIndex: "99"
        },
        opts = $.extend(defaults, options);
        $(".test").remove();
        return this.each(function () {
            if ("placeholder" in document.createElement("input")) return;
            $(this).parent().css("position", "relative");
//            var isIE = $.browser.msie,
//            version = $.browser.version;
            if(!$(this).is(":hidden")){//不支持placeholder的浏览器
                var $this = $(this),
                    msg = $this.attr("placeholder"),
                    iH = $this.outerHeight(),
                    iW = $this.outerWidth(),
                    iX = $this.position().left,
                    iY = $this.position().top,
                    oInput = $("<label>", {
                    "class": "test",
                    "text": msg,
                    "css": {
                        "position": "absolute",
                        "left": iX + "px",
                        "top": iY + "px",
                        "width": iW - opts.posL + "px",
                        "padding-left": opts.posL + "px",
                        "height": iH + "px",
                        "line-height": iH + "px",
                        "color": opts.pColor,
                        "font-size": opts.pFont,
                        "z-index": opts.zIndex,
                        "cursor": "text"
                    }
                    }).insertBefore($this);
                //初始状态就有内容
                var value = $this.val();
                if (value.length > 0) {
                	oInput.hide();
                };
                $this.on("focus", function () {
                    oInput.hide();
                    oInput.css("color", opts.pActive);
                }).on("blur", function () {
                    var value = $(this).val();
                    if (value.length == 0) {
                        oInput.css("color", opts.pColor).show();
                    }
                });
                oInput.on("click", function () {
                    $this.trigger("focus");
                    $(this).css("color", opts.pActive);
                });
                $this.filter(":focus").trigger("focus");
            }
        });
    };
})(jQuery);
时间: 2024-08-07 00:25:16

js模拟placeholder效果1的相关文章

360浏览器搜索框下拉选择图片js模拟select效果

最近360浏览器网址导航的主页增加了一个下拉选择图片搜索的功能,也就是用js模拟出了select的效果,今天在单位闲了无事干,就把空上功能给摸索出来了,虽然做的不是太完善,但对要求不高的用户来说,已经可以了,而且也可以为学习Js的朋友提供参考. <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <style>

在IE8等不支持placeholder属性的浏览器中模拟placeholder效果

placeholder是一个很有用的属性,可以提示用户在input框中输入正确的内容,但是IE8以及IE8一下的浏览器不支持该属性,我们可以使用js来模拟相似的效果.下面直接上代码: <!doctype html> <html> <header> <meta charset="utf-8"> <title>placeholder</title> <style type="text/css"

JS实现placeholder效果

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>placeholder效果</title> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.js"

jQuery封装placeholder效果,让低版本浏览器支持该效果

页面中的输入框默认的提示文字一般使用placeholder属性就可以了,即: <input type="text" name="username" placeholder="请输入用户名" value="" id="username"/> 最多加点样式控制下默认文字的颜色 input::-webkit-input-placeholder{color:#AAAAAA;} 但是在低版本的浏览器却不支

静态书架和js模拟翻书效果

书籍图片随便找了个,有点难看,须要的自己替换个好看点的png格式图片 源代码下载:http://download.csdn.net/detail/sweetsuzyhyf/7604091 静态书架和js模拟翻书效果,布布扣,bubuko.com

Jquery简单的placeholder效果

Jquery简单的placeholder效果 由于IE6-IE9不支持HTML5中的placeholder,所以自己依赖于Jquery简单的写了一个,供参考! 先看看效果吧!如下JSFiddle地址 查看效果链接 JS代码如下: /* * JS placeholder * IE6-IE9不支持HTML5中的placeholder */ function Placeholder(options) { this.config = { defaultColor: '#ccc', curColor: '

IE下支持文本框和密码框placeholder效果的JQuery插件

基于jQuery实现的,主要用于IE下实现placeholder效果,可同时支持文本和密码输入框.placeholder是HTML5新增的一个属性,当input设置了该属性后,该值的内容将作为灰色提示显示在文本框中,当文本框获得焦点时,提示文字消失. 实现代码如下: (function($) {  /**   * 没有开花的树   * 2012/11/28 15:12   */ var placeholderfriend = {    focus: function(s) {      s =

跨浏览器实现placeholder效果的jQuery插件

曾经遇到这样一个问题,处理IE8密码框placeholder属性兼容性.几经周折,这个方案是可以解决问题的. 1.jsp页面引入js插件 <script type="text/javascript" src="<%=basePath%>/login.js" ></script> 2.页面初始化代码 <script type="text/javascript"> //input提示信息 效果插件 针对

获取表单的初始值,模拟placeholder属性

input和textarea有一个默认属性defaultValue,即初始值. 即使在页面操作修改了input和textarea的内容,获取到的defaultValue依然是初始值.可通过该值模拟placeholder属性,而不额外添加任何属性,标签之类. js:  以input为例 jq: 完全模拟placeholder,无需添加任何额外属性和标签. textarea标签模拟过程完全一致: