onFocus .css and onblur .css
onFocus .css and onblur .css
我的HTML有一个占位符,比如:
<input id="additionalsearch" type="text" value="Search Within" onfocus="if (this.value=='Search Within')this.value='';" onblur="if (this.value=='')this.value='Search Within';">
这不是我的选择,我真的不能删除它…但我想知道如何设置CSS。。。我想做一些类似onfocus="if (this.value=='Search Within')this.value=''; $(this).css('color','000000');"
和onblur="if (this.value=='')this.value='Search Within';$(this).css('color', 'A9A9A9');"
的事情。我怎样才能做到这一点?
感谢
您可以使用伪选择器:
input[type=text]:focus {
color: black;
}
input[type=text] {
color: gray;
}
实际上,除了两个错误之外,您几乎提供了代码:
- 你需要在你的颜色之前加一个#
- 你需要用大括号括条件句
其他一切都很好(我改变了颜色,让发生的事情更加明显):
<input id="additionalsearch" type="text" value="Search Within" onfocus="if (this.value=='Search Within'){this.value='';$(this).css('color','#00ff00');}" onblur="if (this.value==''){this.value='Search Within';$(this).css('color', '#ff0000');}">
http://jsfiddle.net/A4cuy/6/
当然,这最终会变成非常难看的代码,建议您将javascript放在单独的处理程序中。
您可以使用pseudo-class:focus作为颜色http://www.w3schools.com/cssref/sel_focus.asp
至于"Search Within",我建议使用defaultText jQuery插件
http://archive.plugins.jquery.com/project/defaultInputText
您可以使用以下
$('#additionalsearch')
.bind('focus',function(){
if (this.value=='Search Within'){
this.value='';
};
$(this).css('color','black')})
.bind('blur',function(){
if (this.value==''){
this.value='Search Within';
}
$(this).css('color','gray')});
品尝这个:
<input id="fldnm" style="background:#B6FF00" type="text" value="Search"
onfocus="if(this.value=='Search'){this.value=''}this.style='background:#FF7F7F';"
onblur="if(this.value==''){this.value='Search'this.style='background:#B6FF00';}else{};"
/>
相关文章:
- CSS-如何定位内容数据标题
- 窗口大小html css
- 如果文本字段为空,则使用JavaScript应用CSS样式
- 动画.CSS重播
- 有没有一种方法可以防止img get请求使用css或js发生
- CSS表格:从列平移到整个表格宽度
- CKEditor-我在editor.css中的风格是't
- 单击更改图标并通过javascript添加一个css类
- 将CSS应用于printWindow.print();在Javascript中
- 使用CSS或js,使用动态选择器选择任意li的下一个元素
- CSS-若窗口太小,滚动条会出现在“表格”单元格上
- css(或jQuery)悬停时淡入淡出
- 为什么不'在JQuery中找到第二个css选择器的工作
- 为什么 .focus() 不起作用,而 .css(“color”,“red”) 在同一个选择器上起作用
- 如何使用css动画/javascript使具有背景图像的元素出现
- 获取HTML属性中CSS声明的值
- 当我更改Joomla时,它仍然从旧域加载CSS和Javascript
- 如何使用css动画从中心增加边界线
- 谷歌图表-如何更改整个表的css属性
- onFocus .css and onblur .css