頁面中的輸入框預設的提示文字一般使用placeholder
屬性就可以了,即:
<input>
最多加點樣式控制下預設文字的顏色
input::-webkit-input-placeholder{color:#AAAAAA;}
但是低版的瀏覽器卻不支援這個placeholder
屬性,那麼真的要在低版瀏覽器也要實作跟placeholder
一樣的效果,就需要寫個插件來相容下,下面就細講一下怎樣用jquery來實現這個模擬效果。
實現這個模擬效果,頁面的一般呼叫方式:
$('input').placeholder();
#首先,先寫jquery外掛的一般結構:
;(function($){ $.fn.placeholder = function(){//实现placeholder的代码 } })(jQuery)
下面我們就要判斷瀏覽器是否支援placeholder屬性
。
;(function($){ $.fn.placeholder = function(){this.each(function(){var _this = this;var supportPlaceholder = 'placeholder' in document.createElement('input');if(!supportPlaceholder){//不支持placeholder属性的操作} }); } })(jQuery)
我們要支援鍊式操作,如下:
;(function($){ $.fn.placeholder = function(){ return this.each(function(){var _this = this;var supportPlaceholder = 'placeholder' in document.createElement('input');if(!supportPlaceholder){//不支持placeholder属性的操作} }); } })(jQuery)
預設組態項目:
options = $.extend({ placeholderColor:'#aaaaaa', isSpan:false, //是否使用插入span标签模拟placeholder的方式,默认是不需要onInput:true //实时监听输入框},options);
#如果不需要透過span來模擬placeholder
效果,那麼就需要透過輸入框的value值來判斷,如下程式碼:
if(!options.isSpan){ $(_this).focus(function () {var pattern = new RegExp("^" + defaultValue + "$|^$"); pattern.test($(_this).val()) && $(_this).val('').css('color', defaultColor); }).blur(function () {if($(_this).val() == defaultValue) { $(_this).css('color', defaultColor); }else if($(_this).val().length == 0) { $(_this).val(defaultValue).css('color', options.placeholderColor) } }).trigger('blur'); }
如果需要同span標籤來模擬placeholder
效果,程式碼如下:
var $simulationSpan = $('<span>'+defaultValue+'</span>'); $simulationSpan.css({'position':'absolute','display':'inline-block','overflow':'hidden','width':$(_this).outerWidth(),'height':$(_this).outerHeight(),'color':options.placeholderColor,'margin-left':$(_this).css('margin-left'),'margin-top':$(_this).css('margin-top'),'padding-left':parseInt($(_this).css('padding-left')) + 2 + 'px','padding-top':_this.nodeName.toLowerCase() == 'textarea' ? parseInt($(_this).css('padding-top')) + 2 : 0,'line-height':_this.nodeName.toLowerCase() == 'textarea' ? $(_this).css('line-weight') : $(_this).outerHeight() + 'px','font-size':$(_this).css('font-size'),'font-family':$(_this).css('font-family'),'font-weight':$(_this).css('font-weight') });//通过before把当前$simulationSpan添加到$(_this)前面,并让$(_this)聚焦$(_this).before($simulationSpan.click(function () { $(_this).trigger('focus'); }));//当前输入框聚焦文本内容不为空时,模拟span隐藏$(_this).val().length != 0 && $simulationSpan.hide();if (options.onInput) {//绑定oninput/onpropertychange事件var inputChangeEvent = typeof(_this.oninput) == 'object' ? 'input' : 'propertychange'; $(_this).bind(inputChangeEvent, function () { $simulationSpan[0].style.display = $(_this).val().length != 0 ? 'none' : 'inline-block'; }); }else { $(_this).focus(function () { $simulationSpan.hide(); }).blur(function () {/^$/.test($(_this).val()) && $simulationSpan.show(); }); };
#整體程式碼:
;(function($){ $.fn.placeholder = function(options){ options = $.extend({ placeholderColor:'#aaaaaa', isSpan:false, //是否使用插入span标签模拟placeholder的方式,默认是不需要onInput:true //实时监听输入框 },options); return this.each(function(){var _this = this;var supportPlaceholder = 'placeholder' in document.createElement('input');if(!supportPlaceholder){//不支持placeholder属性的操作var defaultValue = $(_this).attr('placeholder');var defaultColor = $(_this).css('color');if(!options.isSpan){ $(_this).focus(function () {var pattern = new RegExp("^" + defaultValue + "$|^$"); pattern.test($(_this).val()) && $(_this).val('').css('color', defaultColor); }).blur(function () {if($(_this).val() == defaultValue) { $(_this).css('color', defaultColor); }else if($(_this).val().length == 0) { $(_this).val(defaultValue).css('color', options.placeholderColor) } }).trigger('blur'); }else{var $simulationSpan = $('<span>'+defaultValue+'</span>'); $simulationSpan.css({'position':'absolute','display':'inline-block','overflow':'hidden','width':$(_this).outerWidth(),'height':$(_this).outerHeight(),'color':options.placeholderColor,'margin-left':$(_this).css('margin-left'),'margin-top':$(_this).css('margin-top'),'padding-left':parseInt($(_this).css('padding-left')) + 2 + 'px','padding-top':_this.nodeName.toLowerCase() == 'textarea' ? parseInt($(_this).css('padding-top')) + 2 : 0,'line-height':_this.nodeName.toLowerCase() == 'textarea' ? $(_this).css('line-weight') : $(_this).outerHeight() + 'px','font-size':$(_this).css('font-size'),'font-family':$(_this).css('font-family'),'font-weight':$(_this).css('font-weight') });//通过before把当前$simulationSpan添加到$(_this)前面,并让$(_this)聚焦$(_this).before($simulationSpan.click(function () { $(_this).trigger('focus'); }));//当前输入框聚焦文本内容不为空时,模拟span隐藏$(_this).val().length != 0 && $simulationSpan.hide();if (options.onInput) {//绑定oninput/onpropertychange事件var inputChangeEvent = typeof(_this.oninput) == 'object' ? 'input' : 'propertychange'; $(_this).bind(inputChangeEvent, function () { $simulationSpan[0].style.display = $(_this).val().length != 0 ? 'none' : 'inline-block'; }); }else { $(_this).focus(function () { $simulationSpan.hide(); }).blur(function () {/^$/.test($(_this).val()) && $simulationSpan.show(); }); }; } } }); } })(jQuery);
呼叫方式,需要透過span標籤來模擬的話:
$("#username").placeholder({ isSpan:true});
以上是jQuery封裝placeholder的實例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

要在UbuntuLinux中删除FirefoxSnap,可以按照以下步骤进行操作:打开终端并以管理员身份登录到Ubuntu系统。运行以下命令以卸载FirefoxSnap:sudosnapremovefirefox系统将提示你输入管理员密码。输入密码并按下Enter键以确认。等待命令执行完成。一旦完成,FirefoxSnap将被完全删除。请注意,这将删除通过Snap包管理器安装的Firefox版本。如果你通过其他方式(如APT包管理器)安装了另一个版本的Firefox,则不会受到影响。通过以上步骤

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

mozilla firefox可以卸载;firefox属于第三方浏览器,如果不需要,完全可以卸载。卸载方法:1、在开始菜单中,依次点击“Windwos系统”-“控制面板”;2、在“控制面板”界面中,点击“程序和功能”;3、在新界面中,找到并双击火狐浏览器图标;4、在卸载弹窗中,点击“下一步”;5、点击“卸载”即可。

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3漢化版
中文版,非常好用

Dreamweaver Mac版
視覺化網頁開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。