html5代码:
<div class="ub-f3 ub-ac search_input uc-a1 sc-bg-active uinput ub ub-f1 bc-border uba">
<input οninput="toggleIcon(this);" type="text" οnfοcus="toggleIcon(this);" οnblur="lost(this);" placeholder="关键字:姓名" class="ub-f1">
<span class="ub-img ub clear uhide"></span>
</div>
JS代码:
function toggleIcon(ele){
var inputVal = ele.value;
var clearEle = ele.nextElementSibling;
if(inputVal == ''){
clearEle.className = 'ub-img ub clear uhide';
} else {
clearEle.className = 'ub-img ub clear';
}
}
// 输入框失去焦点事件
function lost(ele){
var inputVal = ele.value;
var clearEle = ele.nextElementSibling;
setTimeout( function(){ clearEle.className = 'ub-img ub clear uhide'; }, 200);
}
function clearInput(ele){
var inputEle = ele.previousElementSibling;
inputEle.value = '';
ele.className = 'ub-img ub clear uhide';
inputEle.focus();
}
转载于:https://blog.51cto.com/10145212/1669120