javascript - 使背景在用户输入时消失。 CSS

标签 javascript html css

如果我有这段代码:

<input style="background: src(mybackgroundimage.png) no-repeat right" type="text">

如果用户在文本框中输入内容,如何让背景消失。

最佳答案

您可以使用 input事件:

// Assuming: <input class="inp" id="inp" type="text">
document.getElementById('inp').addEventListener('input', function() {
    if (this.value) {
        this.style.background = 'none';
    }
}, false);

注:IE9+。

演示:http://jsfiddle.net/N3kaR/

如果您需要 ie 支持,您可以这样做:

var inp = document.getElementById('inp');
inp.addEventListener('change', clearBackground, false);
inp.addEventListener('paste', clearBackground, false);
inp.addEventListener('keyup', clearBackground, false);

function clearBackground() {
    this.style.background = 'none';
}

演示:http://jsfiddle.net/N3kaR/1/

关于javascript - 使背景在用户输入时消失。 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21495580/

相关文章:

javascript - 在 jquery 中选择时显示复选框值

html - WCAG 文本间距插件

html - 在不将 flex 应用于 CSS 元素的情况下设置折叠优先级?

javascript - 为什么我不能在数组中的一行中检测到 3 个奇数?

javascript - 浏览器中性方式向 javascript 中的选择元素添加选项

javascript - 奇怪的矛盾错误

javascript - 使用 javascript 添加(而不是替换)css 类

javascript - 将导航中的下拉子菜单对齐到与其父级相同的高度

css - 显示 : table-cell causing firefox and opera to ignore max-width and max-height properties

javascript - 在 contenteditable div 中不允许超过 1 个字符