html - CSS3 可扩展搜索框不能与占位符一起正常工作

标签 html css transition placeholder

如果未单击搜索图标,我想隐藏占位符。这可能吗?如果可能的话,我该如何解决这个问题?

HTML

<form id="demo-2">
  <input type="search" placeholder="some text" />
</form>

CSS

input { outline: none; }
input[type=search] {
    font-family: inherit; 
    font-size: 100%; 
    -webkit-appearance: textfield; 
    -webkit-box-sizing: content-box;
}

input::-webkit-search-decoration, input::-webkit-search-cancel-button
   { display: none; }

input[type=search] { 
    background:url(http://static.tumblr.com/ftv85bp/MIXmud4tx/search-icon.png) no-repeat 9px; 
    border:none; 
    height:35px; 
    -moz-transition: all .5s; 
    transition: all .5s; }


#demo-2 input[type=search] {
    width: 100px;
    padding-left: 10px;
    color: transparent;
    cursor: pointer;
}

#demo-2 input[type=search]:focus {
    background:url(http://static.tumblr.com/ftv85bp/MIXmud4tx/search-icon.png) no-repeat 9px center;
    width: 626px;
    padding-left: 32px;
    color: #000;
    background-color: #fff;
    cursor: auto;
}


#demo-2 input:-moz-placeholder { color:red; }
#demo-2 input::-webkit-input-placeholder { color:red; }

演示在 JSFiddle

抱歉我的英语不好。感谢您的任何回答。

最佳答案

尝试将 onfocusonblur 属性添加到 input:

<input type="search" onfocus="placeholder='some text'" onblur="placeholder=''" />

fiddle :http://jsfiddle.net/dW92N/3/

关于html - CSS3 可扩展搜索框不能与占位符一起正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23276439/

相关文章:

jquery - 如何使用 jQuery 在离开页面时添加页面过渡

icons - 如何在 Font Awesome 图标中创建过渡效果

javascript - 如何在 JavaScript 中实现 "add new item"功能?

javascript - addEventListener 与 onClick 属性的缺点

javascript - 是否可以使用 css 媒体查询获取移动设备的大小并将其发送到 php 变量

javascript - 我正在尝试获取此元素 margin-top 并简单地更改它

html - 防止 Angular 中的 div 溢出

javascript - jQuery 获取子级的精确父级(使用 UL 和 LI)

html - 为什么两个 div 没有定位就相互重叠?

html - 如何同时在框上悬停和过渡?只使用 css 和 html