js實(shí)現(xiàn)Element中input組件的部分功能并封裝成組件(實(shí)例代碼)
現(xiàn)在實(shí)現(xiàn)的有基礎(chǔ)用法、可清空、密碼框,參考鏈接:https://element.eleme.cn/#/zh-CN/component/input
HTML代碼:想要測試哪個(gè)組件,直接將對應(yīng)組件解開注釋即可,標(biāo)紅的js和css記得修改成你自己的位置。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>js實(shí)現(xiàn)可清空input組件</title> <script src="../js/input/jsInput.js"></script> <link rel="stylesheet" type="text/css" href="../css/jsInput.css"/> </head> <body> <script> //普通input輸入框 document.write(createElementInput()) //添加可清空功能clearable //document.write(createElementInput("clearable")) //實(shí)現(xiàn)密碼框show-password //document.write(createElementInput("show-password")) </script> </body> </html>
JS代碼:
function createElementInput(str){ var temp = str; var html = "<div id='my_input_div' onmouseover='addClearNode(\""+str+"\")'' onmouseout='hiddenClearNode(\""+str+"\")''>"; html += "<input id='my_input' placeholder='請輸入內(nèi)容'"; if(str){ if(str == 'show-password'){ html+=" type = 'password' "; } } html += "oninput='addClearNode(\""+str+"\")'"; html += "onclick='changeColor(\""+str+"\")'"; html += "onblur='hiddenClearNode(\""+str+"\")'/>"; if(str){ html += "<input id='"+str+"' onmousedown='changeValue(\""+str+"\")'/>"; } html += "</div>" return html; } //box-shadow: 0 0 0 20px pink; 通過添加陰影的方式顯示邊框 function changeColor(str){ //alert(str) document.getElementById("my_input_div").style.boxShadow="0 0 0 2px #409eff"; //獲取inpu的值 var value = document.getElementById('my_input').value; var button = document.getElementById(str); //添加判斷 如果輸入框中有值 則顯示清空按鈕 if(value){ if(button){ button.style.visibility = "visible" } } } //應(yīng)該輸入內(nèi)容之后使用該事件 function addClearNode(str){ var value = document.getElementById('my_input').value; var button = document.getElementById(str); //alert(value) if(value){ if(button){ //將button設(shè)置為可見 button.style.visibility = 'visible' } }else{ //判斷該屬性是否存在 if(button){ //將button設(shè)置為不可見 button.style.visibility = 'hidden' } } //選中后div添加選中樣式 高亮顯示 document.getElementById("my_input_div").style.outline="0 0 0 2px #409eff"; } //改變input中的值 function changeValue(str){ if(str){ if(str == 'clearable'){ clearValues(str); }else if(str == 'show-password'){ showPassword(); } } } //清空輸入值 function clearValues(str){ document.getElementById("my_input").value = ""; document.getElementById(str).style.visibility = "hidden"; //仍然處于選中狀態(tài) div邊框突出陰影 document.getElementById("my_input_div").style.boxShadow="0 0 0 2px #409eff" } //隱藏清除按鈕 function hiddenClearNode(str){ var button = document.getElementById(str); if(button){ button.style.visibility="hidden"; } //將div陰影設(shè)置為0 document.getElementById("my_input_div").style.boxShadow="0 0 0" } //顯示密碼 function showPassword(){ var myInput = document.getElementById('my_input'); var password = myInput.value; var type = myInput.type; //alert(type) if(type){ if(type == 'password'){ myInput.type = ''; myInput.value = password; }else{ myInput.type = 'password'; myInput.value = password; } } //仍然處于選中狀態(tài) div邊框突出陰影 document.getElementById("my_input_div").style.boxShadow="0 0 0 2px #409eff" }
CSS代碼:
#my_input_div{ width: 150px; border: 1px solid silver; border-radius: 4px; position: relative; } #my_input{ height: 30px; width:100px; margin-left: 6px; border: none; outline: none; cursor: pointer; } #clearable{ height: 20px; width: 15px; text-align: center; visibility:hidden; border: none; outline: none; color: #409eff; cursor: pointer; background-image: url(../image/clear.svg); background-repeat: no-repeat; background-size: 12px; position: absolute; top: 10px; left: 120px; display: inline-block; } #show-password{ height: 20px; width: 15px; text-align: center; visibility:hidden; border: none; outline: none; color: #409eff; cursor: pointer; background-image: url(../image/eye.svg); background-repeat: no-repeat; background-size: 12px; position: absolute; top: 10px; left: 120px; display: inline-block; }
剩下的功能會慢慢被完善......
到此這篇關(guān)于純生js實(shí)現(xiàn)Element中input組件的部分功能(慢慢完善)并封裝成組件的文章就介紹到這了,更多相關(guān)js實(shí)現(xiàn)input組件功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- JavaScript axios安裝與封裝案例詳解
- 深入淺析同源與跨域,jsonp(函數(shù)封裝),CORS原理
- vue.js管理后臺table組件封裝的方法
- 詳解JavaScript面向?qū)ο髮?shí)戰(zhàn)之封裝拖拽對象
- 原生js封裝無縫輪播功能
- 原生JS封裝vue Tab切換效果
- js實(shí)現(xiàn)封裝jQuery的簡單方法與鏈?zhǔn)讲僮髟斀?/a>
- JavaScript實(shí)現(xiàn)原型封裝輪播圖
- JavaScript緩動動畫函數(shù)的封裝方法
- javascript canvas封裝動態(tài)時(shí)鐘
- 關(guān)于Jackson的JSON工具類封裝 JsonUtils用法
- JavaScript封裝單向鏈表的示例代碼
- 常用的前端JavaScript方法封裝
相關(guān)文章
ECharts數(shù)據(jù)可視化基本使用之常用圖表類型
這篇文章主要給大家介紹了關(guān)于ECharts數(shù)據(jù)可視化基本使用之常用圖表類型的相關(guān)資料,echarts是一款基于JavaScript的數(shù)據(jù)可視化圖表庫,提供直觀,生動,可交互,可個(gè)性化定制的數(shù)據(jù)可視化圖表,需要的朋友可以參考下2023-11-11用JS寫的簡單的計(jì)算器實(shí)現(xiàn)代碼
參加某公司的面試后,有一機(jī)試題目:用web技術(shù)開發(fā)一個(gè)B/S結(jié)構(gòu)的公式解析器。于是想了想思路,神來一筆想先寫個(gè)計(jì)算器程序做基礎(chǔ),于是便寫了這個(gè)程序。2009-09-09JS實(shí)現(xiàn)數(shù)組去重,顯示重復(fù)元素及個(gè)數(shù)的方法示例
這篇文章主要介紹了JS實(shí)現(xiàn)數(shù)組去重,顯示重復(fù)元素及個(gè)數(shù)的方法,涉及javascript數(shù)組遍歷、統(tǒng)計(jì)、計(jì)算等相關(guān)操作技巧,需要的朋友可以參考下2019-01-01如何自己實(shí)現(xiàn)JavaScript的new操作符
new大家肯定都不陌生,單身沒有對象的時(shí)候就new一個(gè),很方便。那么它在創(chuàng)建實(shí)例的時(shí)候,具體做了哪些操作呢?今天我們就來一起分析一下。2021-04-04javascript將非數(shù)值轉(zhuǎn)換為數(shù)值
parseInt()不能轉(zhuǎn)換浮點(diǎn)型數(shù)值,我們用parseFloat()來解決。這篇文章主要介紹了javascript將非數(shù)值轉(zhuǎn)換為數(shù)值,需要的朋友可以參考下2018-09-09JS實(shí)現(xiàn)自定義簡單網(wǎng)頁軟鍵盤效果代碼
這篇文章主要介紹了JS實(shí)現(xiàn)自定義簡單網(wǎng)頁軟鍵盤效果代碼,可實(shí)現(xiàn)簡單模擬鍵盤布局及響應(yīng)鼠標(biāo)點(diǎn)擊按下鍵盤按鍵功能,簡單實(shí)用,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11Javascript實(shí)現(xiàn)秒表倒計(jì)時(shí)功能
這篇文章主要為大家詳細(xì)介紹了Javascript實(shí)現(xiàn)秒表倒計(jì)時(shí)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11