html - 如何在输入不为空时仅使用 css 永久隐藏搜索图标

标签 html css

我在每个输入文本框中应用搜索图标。 当图标具有一定值(value)时,我的问题就出现了。

你可以在这里看到它-

Fiddle

当我聚焦时,搜索图标会隐藏,但我该如何管理,以便在它有一些值(value)时一直隐藏这个图标?

css-

.input-search {
    background:url('http://www.aljanaedu.com/Limitless/images/icons/14x14/search.png') no-repeat left 10px center;
}
    .input-search:focus {
        background-image:none;
    }

我可以很容易地用 jQuery 做到这一点,但是有什么方法可以只用 CSS 做到这一点吗?

最佳答案

我已经更新了你的 fiddle ,你可以试试看! http://jsfiddle.net/X7k2B/6/

HTML:

<input 
    type="text" 
    id="myInput" 
    placeholder="Some Fallback Text" >

CSS:

#myInput::-webkit-input-placeholder {
  color: transparent;
  text-indent: -9999px;
   background:url('http://www.aljanaedu.com/Limitless/images/icons/14x14/search.png') no-repeat left 10px center;
  background-position: 0 50%;
  background-repeat: no-repeat; 
}
#myInput::-moz-placeholder {
  /* Firefox 19+ */
  color: transparent;
  text-indent: -9999px;
  background-image: url("http://placehold.it/123x17&text=PlaceholderImage");
  background-position: 0 50%;
  background-repeat: no-repeat; 
}
#myInput:-moz-placeholder {
  /* Firefox 18- */
  color: transparent;
  text-indent: -9999px;
  background-image: url("http://placehold.it/123x17&text=PlaceholderImage");
  background-position: 0 50%;
  background-repeat: no-repeat; 
}
#myInput:-ms-input-placeholder {
  /* IE 10- */
  color: transparent;
  text-indent: -9999px;
  background-image: url("http://placehold.it/123x17&text=PlaceholderImage");
  background-position: 0 50%;
  background-repeat: no-repeat; 
}

关于html - 如何在输入不为空时仅使用 css 永久隐藏搜索图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24503240/

相关文章:

javascript - 到达新的 div 时停止粘性 div

javascript - mdl-menu material-design-lite 中的 mdl-textfield

javascript - Jquery Fade In 无法在 Safari 中工作

html - 处理不同浏览器中不支持的游标

html - 高度随内容展开

javascript - 单击它调整循环云图像的大小

javascript - 单击 anchor 标记时添加效果

CSS 字体属性语法,指定没有 px 或单位的大小

javascript - 如果 URL 中没有片段标识符,网站将无法完全加载

javascript - 从 iframe 外部通过 CSS Path 访问元素