js實現(xiàn)Element中input組件的部分功能并封裝成組件(實例代碼)
現(xiàn)在實現(xiàn)的有基礎(chǔ)用法、可清空、密碼框,參考鏈接:https://element.eleme.cn/#/zh-CN/component/input



HTML代碼:想要測試哪個組件,直接將對應(yīng)組件解開注釋即可,標紅的js和css記得修改成你自己的位置。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>js實現(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"))
//實現(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實現(xiàn)Element中input組件的部分功能(慢慢完善)并封裝成組件的文章就介紹到這了,更多相關(guān)js實現(xiàn)input組件功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- JavaScript axios安裝與封裝案例詳解
- 深入淺析同源與跨域,jsonp(函數(shù)封裝),CORS原理
- vue.js管理后臺table組件封裝的方法
- 詳解JavaScript面向?qū)ο髮崙?zhàn)之封裝拖拽對象
- 原生js封裝無縫輪播功能
- 原生JS封裝vue Tab切換效果
- js實現(xiàn)封裝jQuery的簡單方法與鏈式操作詳解
- JavaScript實現(xiàn)原型封裝輪播圖
- JavaScript緩動動畫函數(shù)的封裝方法
- javascript canvas封裝動態(tài)時鐘
- 關(guān)于Jackson的JSON工具類封裝 JsonUtils用法
- JavaScript封裝單向鏈表的示例代碼
- 常用的前端JavaScript方法封裝
相關(guān)文章
解決JS組件bootstrap table分頁實現(xiàn)過程中遇到的問題
這篇文章主要介紹了JS組件bootstrap table分頁實現(xiàn)過程中遇到的問題,感興趣的小伙伴們可以參考一下2016-04-04
JavaScript實現(xiàn)彈出DIV層同時頁面背景漸變成半透明效果
這篇文章主要介紹了JavaScript實現(xiàn)彈出DIV層同時頁面背景漸變成半透明效果,涉及JavaScript彈出窗口的實現(xiàn)及頁面元素屬性動態(tài)變換的相關(guān)技巧,需要的朋友可以參考下2016-03-03
利用select實現(xiàn)年月日三級聯(lián)動的日期選擇效果【推薦】
關(guān)于select控件,可能年月日三級聯(lián)動的日期選擇效果是最常見的應(yīng)用了。本文將對日期選擇效果進行詳細介紹。需要的朋友一起來看下吧2016-12-12
javascript 構(gòu)建一個xmlhttp對象池合理創(chuàng)建和使用xmlhttp對象
在我的這篇舊文里曾經(jīng)發(fā)布了一個簡單的ajax操作類。我們發(fā)現(xiàn),在舊文里創(chuàng)建xmlhttp對象的時候,每次都要new一個對象。而我們都知道new一個對象的開銷是很大的。2010-01-01
詳解webpack之圖片引入-增強的file-loader:url-loader
這篇文章主要介紹了詳解webpack之圖片引入-增強的file-loader:url-loader,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10
sass 中使用 /deep/ 修改 elementUI 組件樣式報錯
這篇文章主要介紹了sass 中使用 /deep/ 修改 elementUI 組件樣式報錯的解決方案,嘗試用 ::v-deep 替換 /deep/ ,成功解決了問題,感興趣的朋友跟隨小編一起看看吧2024-02-02

