placeholder的兼容處理方法

placeholder是html5新增的一個屬性,極大的減輕了表單提示功能的實現,但是對于IE6-IE9真的是只能靠自己寫啦!

但是在自己寫時會掉進了一個坑里,還好用了一會時間還是爬出來啦。

最終的解決方法方法如下:

 1 <form name="doluform" id="loginform">
 2     <div class="inputdivin">
 3         <input type="text" placeholder="用戶名" name="usernamez" value=""/>
 4         <p class="tipsdiv"><span id="logintipsp"></span></p>
 5     </div>
 6     <div class="inputdivin">
 7         <input type="password" placeholder="密碼" name="passwordz" value=""/>
 8         <span style="display:none" class="placespan"></span>
 9         <p class="tipsdiv"><span id="loginposswordtipsp"></span></p>
10     </div>
11     <div class="inputivin">
12         <p id="jhemail"></p>
13     </div>
14     <div class="inputdivin">
15         <button type="submit" title="點擊登錄" class="submitbtn">登&nbsp;錄</button>
16     </div>
17     <div class="inputdivin">
18         <p class="passwordbc"><a href="javascript:void(0);" class="passwordaction" id="holdpassword"><b class="test">&#xe602;</b>&nbsp;&nbsp;記住密碼<input type="hidden" name="remfor_input" value="1"/></a></p>
19         <p class="findpassword"><a href="${ctx}/home/index/findpassword_step1.jsp" class="passwordaction">忘記密碼</a></p>
20     </div>
21 </form>
View Code

?

而對應的js組件寫法如下:

 1 var Inputplace=function(){
 2     this.obj=null;
 3     this.type="";
 4     this.color="";
 5     this.tipspan=null;
 6 }
 7 Inputplace.prototype.init=function(obj,opt){
 8     var This=this;
 9     this.obj=obj;
10     this.setting={
11         "defaultz":obj.attr("placeholder"),
12         "tccolor":"#6d696a"
13     }
14     $.extend(this.setting,opt);
15     this.oldcolor=obj.css("color");
16     this.type=obj.attr("type");
17     if(this.type=="text"){//文本input設置
18         this.obj.val(this.setting.defaultz).css("color",this.setting.tccolor);
19         this.obj.unbind("focus");
20         this.obj.focus(function(){
21             if(This.obj.val()==This.setting.defaultz){
22                 This.obj.val("").css("color",This.oldcolor);
23             }
24         })
25         this.obj.unbind("blur");
26         this.obj.blur(function(){
27             if(This.obj.val()=="" || /^\s*$/.test(This.obj.val())){
28                 This.obj.val(This.setting.defaultz).css("color",This.setting.tccolor);
29             }
30         })
31     }
32     else if(this.type=="password"){//密碼input實現
33         this.tipspan=this.obj.next("span.placespan");
34         this.tipspan.show().html(this.setting.defaultz).css({"color":this.setting.tccolor});
35         this.obj.unbind("focus");
36         this.obj.focus(function(){
37             This.tipspan.hide();
38         })
39         this.tipspan.unbind("click");
40         this.tipspan.click(function(){
41             $(this).hide();
42             This.obj.focus();
43         })
44         this.obj.unbind("blur");
45         this.obj.blur(function(){
46             if(This.obj.val()=="" || /^\s$/.test(This.obj.val())){
47                 This.tipspan.show();
48             }
49         })
50     }
51 }
View Code

而調用方法如下:

 1 <!--[if lte IE 9 ]>
 2 <script type="text/javascript" src="${ctx}/static/js/common/jsplaceholder.js"></script>
 3 <script type="text/javascript">
 4     $(function(){
 5         var inputtext=$("input:text");
 6         inputtext.each(function(){
 7             new Inputplace().init($(this))
 8         })
 9         var inputpass=$("input:password");
10         inputpass.each(function(){
11             new Inputplace().init($(this))
12         })
13     })
14 </script>
15 <![endif]-->
View Code

其中主要的坑就是在于input的類型上,當input為password的時候,如果你還只是直接設置val來做提示,那你就已經掉坑里啦,因為在password 會把輸入的內容成..的形式,所以得繞一下,如果是password的話可以在password的那一組里新增一個元素去顯示要提示的內容,像其中<span style="display:none" class="placespan"></span>就是專門用來做提示用的,在CSS里要先寫好提示所用到的一干樣式,樣式為tipsdiv的P元素是用來放驗證提示內容的,這里不用管,當表單獲得焦點的時候或者span被點擊的時候span都會消失,而input開始可以輸入內容啦。

<div class="inputdivin"><input type="password" placeholder="密碼" name="registerpassword" value=""/><span style="display:none" class="placespan"></span><p class="tipsdiv"><span id="registerpasswordtipsid"></span></p>
</div>

注:就在html5的placeholder能用的情況下,在各瀏覽器下也會有一些差異,在ff和chrome下,輸入框獲得焦點時,placeholder文字沒有變化,只有當輸入框中輸入了內容時,placeholder文字才消失;而在safari和ie10-ie11下,當輸入框獲得焦點時,placeholder文字便立即消失。

默認情況下,placeholder的文字是灰色,輸入的文字是黑色。而我們拿到的設計稿上的色值往往并不與默認情況下完全一致。那么,如何修改placeholder的色值呢?

如果直接寫input{color:red;},可以看到,ie10和ff下,placeholder文字和輸入文字都變成了紅色.

而在chrome和safari下,placeholder文字顏色不變,只有輸入的文字變成紅色。

顯然,這種做法是行不通的。因為我們只想改變placeholder文字的顏色,并不想改變輸入文字的顏色。
正確的寫法如下:
::-moz-placeholder{color:red;} ? ? ? ? ? ? ?//ff
::-webkit-input-placeholder{color:red;} ? ? //chrome,safari
:-ms-input-placeholder{color:red;} ? ? ? ? ?//ie10

轉載于:https://www.cnblogs.com/xwwin/p/4233338.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/247512.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/247512.shtml
英文地址,請注明出處:http://en.pswp.cn/news/247512.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

常用數據處理

1、樹形數據轉換 在處理商品分類數據、企業列表數據等情況下&#xff0c;后臺會返回到前臺所有的數據。我們需要根據parentId,數據ID將數據轉換為樹形數據進行渲染。 /*** 樹形數據轉換* param {*} data* param {*} id* param {*} pid*/ export function treeDataTranslate(d…

運行Xcode時出現 Lazy loading NSBundle MobileCoreServices.framework和 Loaded MobileCoreServices.framework

運行Xcode時出現 Lazy loading NSBundle MobileCoreServices.framework和 Loaded MobileCoreServices.framework 解決方案&#xff1a; 1、打開項目的 Product-->Scheme --> Edit Scheme--> Run-->Arguments-->Environment Variables添加Name為OS_ACTIVITY_MO…

less中的for循環

.loop(count) when (counter > 0) { .loop((counter - 1)); // 遞歸調用自身width: (10px * counter); // 每次調用時產生的樣式代碼}轉載于:https://www.cnblogs.com/zhouyideboke/p/11178271.html

詳解 vue-cli 的打包配置文件代碼(轉)

一、前言 對于webpack基礎不好&#xff0c;node指令不通的童鞋。估計對自己搭建Vue、react腳手架是相當頭疼的&#xff0c;有種無從下手的感覺。然而&#xff0c;從頭看這2塊&#xff0c;耗時太長&#xff0c;而且說實話得練才行&#xff0c;不練練手看不明白。那大多數人就采取…

iOS之頁面布局-踩坑的原由

iOS之頁面布局 原文請點擊 在《iOS 7 UI Transition Guide》中有在《iOS 7 UI Transition Guide》的Bar and Bar Buttons一節中有這么一段話 In iOS 7, the status bar is transparent, and other bars—that is, navigation bars, tab bars, toolbars, search bars, and sco…

工作中的javascript代碼收集及整理

有個pub庫放在blog的文件夾里面了,注意查查1.用javascript去除字符串左右空格,包括全角和半角//用javascript去除字符串左右空格,包括全角和半角String.prototype.trim function() { //其中表示為&#xff1a;對象.屬性.方法函數方法var strTrim this.replace(/(^\s*)|(\s*$)/…

iOS11 更改狀態欄、導航欄顏色的方法

ios上狀態欄 就是指的最上面的20像素高的部分 狀態欄分前后兩部分&#xff0c;要分清這兩個概念&#xff0c;后面會用到&#xff1a; 前景部分&#xff1a;就是指的顯示電池、時間等部分&#xff1b; 背景部分&#xff1a;就是顯示黑色或者圖片的背景部分&#xff1b; (一)設…

i春秋DMZ大型靶場實驗(四)Hash基礎

下載工具包 打開目標機 通過目錄爆破發現 phpmyadmin 在登錄位置嘗試注入 返現 可以注入 直接上sqlmap 上 bp 代理抓包 sqlmap.py -r bp.txt --dbs 利用sqlmap 跑出root 密碼 root666888 登錄 phpmyadmin t通過路徑報錯得到絕對路徑 c:\\www\\1.php root 權限…

解決MAC系統升級導致COCOAPODS失效問題

使用pod install出現如下錯誤 -bash: /usr/local/bin/pod: /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby: bad interpreter: No such file or directory 這是Mac升級系統導致&#xff0c;當你的Mac系統升級為 high siera的時候&#xff0c;別忘記更新…

ASP.NET中進行消息處理(MSMQ) 二

在我上一篇文章《ASP.NET中進行消息處理(MSMQ)一》里對MSMQ做了個通俗的介紹&#xff0c;最后以發送普通文本消息和復雜的對象消息為例介紹了消息隊列的使用。 本文在此基礎上繼續介紹MSMQ的相關知識點&#xff0c;最后還是通過一個示例程序來分析MSMQ在實際項目開發中的應用。…

js常用的數組方法

js常用的數組方法 1.filter() 不會改變原始數組&#xff0c;新數組中的元素是過濾指定數組中符合條件的所有元素 兩種寫法區別&#xff1a;有return 的加了{}&#xff0c;否則沒有return不需要加{} var aa [1, 2, 3, 4, 4, 5, 6, 6]; var bb aa.filter((item, index, sel…

iOS 適配HTTPS方法

一切為了迎合蘋果 在WWDC 2016開發者大會上&#xff0c;蘋果宣布了一個最后期限&#xff1a;到2017年1月1日 App Store中的所有應用都必須啟用 App Transport Security安全功能。App Transport Security&#xff08;ATS&#xff09;是蘋果在iOS 9中引入的一項隱私保護功能&…

模板—tarjan求割邊

int dfn[MAXN],low[MAXN],cnt; void tarjan(int x,int edg) {low[x]dfn[x]cnt;for(int if(x);i;in(i))if(!dfn[v(i)]){tarjan(v(i),i);low[x]min(low[x],low[v(i)]);if(low[v(i)]>dfn[x])isbridge[i]isbridge[i^1]1;}else if(i!(edg^1))low[x]min(low[x],dfn(v(i))); } 轉載…

GoJs Pictures 官方介紹文檔

圖片 使用Picture類顯示圖像。 最常見的用法是使用URL字符串設置Picture.source屬性&#xff0c;以及通過GraphObject.desiredSize&#xff08;圖對象的所需尺寸&#xff09;獲取或通過設置GraphObject.width&#xff08;圖對象的寬&#xff09;和GraphObject.height&#xff0…

怎樣購買及安裝ssl安全證書

查找資料記錄&#xff0c;不是我的項目筆記 現在越來越多的網站都開始用安全鏈接了&#xff0c;在國外的話&#xff0c;如果不是一個安全鏈接&#xff0c;用戶很大程度上會拒絕使用&#xff0c;所有安全鏈接是未來的趨勢&#xff0c;樓主第一次配安全證書的時候&#xff0c;剛剛…

XmlViewResolver 和 ResourceBundleViewResolver

使用XmlViewResolver 如果視圖對象的 Bean 數目太多&#xff0c;那么直接在 smart-servlet.xml 文件中配置&#xff0c;勢必影響主配置文件的簡潔性。XmlViewResolver 和 BeanNameViewResolver 功能相似&#xff0c;唯一不同的是它可以將視圖 Bean 定義在一個獨立的 XML 文件中…

(轉載)Git使用教程:最詳細、最傻瓜、最淺顯、真正手把手教!

轉載自 Git使用教程 預警&#xff1a;因為詳細&#xff0c;所以行文有些長&#xff0c;新手邊看邊操作效果出乎你的預料&#xff09;一&#xff1a;Git是什么&#xff1f; Git是目前世界上最先進的分布式版本控制系統。 工作原理 / 流程&#xff1a; Workspace&#xff1a;工作…

soureTree中如何設置git 用戶名與密碼 SourceTree提交修改用戶詳細圖文方法

mac上軟件更新&#xff1a; 現在沒有網絡小模塊了&#xff0c;在同行右邊高級里面有默認用戶名刪除即可&#xff01;&#xff01;&#xff01;&#xff01; sourceTree 切換Git登錄用戶&#xff0c;之前在SourceTree提交遠程服務用的是同事的賬號&#xff0c;同事離職后賬號也…

shell 腳本 生成文件,文件名為日期時間

腳本如下 #/bin/bashfilename$(date %Y%m%d)_$(date %H%M%S) touch $filename.txt 其中 $() 表示括號中的 shell 命令的結果&#xff0c;所以 filename 是一個字符串&#xff0c;比如 20190714_111631&#xff0c;即 2019 年 7 月 14 日 11 點 16 分 31 秒。 然后第二行命令&am…