日期:2014-05-16  浏览次数:20408 次

想问下,输入框末尾自动补充清空X的功能是怎么实现的
就是输入框输入数据之后,输入框的末尾自动呈现一个X,点击之后可以将输入框内容全部清空
------解决方案--------------------
那个x其实就是一张图片或者说是一个按钮,点击后输入框内容清空。
用keydown事件检测输入框是否为空,如果不为空则显示清空按钮,如果为空则隐藏
------解决方案--------------------
css控制x显示位置

<style>
.input{position:relative;}
.input .x{display:none;text-decoration:none;position:absolute;left:190px;color:blue;font-weight:bold}
</style>
<div class="input"><a href="#" class="x" onclick="return clearInput(this)">x</a><input type="text" style="width:200px" onkeyup="xShow(this)" /></div>
<script type="text/javascript">
    function clearInput(a) {
        a.parentNode.getElementsByTagName('input')[0].value = '';
        a.style.display = 'none'; 
        return false;
    }
    function xShow(ipt) {
        ipt.parentNode.getElementsByTagName('a')[0].style.display = ipt.value == '' ? 'none' : 'block'
    }
</script>