Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript css和onblur.css_Javascript_Jquery_Css_Onblur_Onfocus - Fatal编程技术网

Javascript css和onblur.css

Javascript css和onblur.css,javascript,jquery,css,onblur,onfocus,Javascript,Jquery,Css,Onblur,Onfocus,“我的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。。。我想做一些类似于on

“我的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',a9a9');“
)。我怎样才能做到这一点


谢谢

您可以使用伪选择器:

input[type=text]:focus {
  color: black;
}

input[type=text] {
  color: gray;
}

您可以使用伪选择器:

input[type=text]:focus {
  color: black;
}

input[type=text] {
  color: gray;
}

可以对颜色使用伪类:focus

至于“搜索范围”,我建议使用defaultText jQuery插件


您可以对颜色使用伪类:焦点

至于“搜索范围”,我建议使用defaultText jQuery插件


实际上,除了两个错误外,您提供的代码基本上都是正确的:

  • 在你的颜色之前,你需要一个#
  • 你需要把条件句用大括号括起来
  • 其他一切都很好(我改变了颜色,使发生的事情更加明显):

    
    


    当然,这最终是非常难看的代码,建议您将javascript放在单独的处理程序中。

    事实上,您提供的代码中除了两个错误外几乎没有其他错误:

  • 在你的颜色之前,你需要一个#
  • 你需要把条件句用大括号括起来
  • 其他一切都很好(我改变了颜色,使发生的事情更加明显):

    
    


    当然,这最终会成为非常难看的代码,建议您将javascript放在单独的处理程序中

    $('#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')});
    

    您可以使用以下命令

    $('#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{};"
       />
    
    
    
    试一试:

    <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{};"
       />
    
    
    
    但问题是,如果有文本,颜色也会改变。我希望颜色表现得像WebKit中的HTML5占位符,您可以显式设置文本的颜色。如果它仍然继承文本框的颜色,请放置一个!在规则末尾很重要。另外,没有:blur伪类(至少我没有找到文档)。对于模糊的情况,只需使用“#additionalsearch”,并让:focus情况覆盖它。但是现在如果用户输入关键字。然后模糊文本框,颜色变为灰色。我只希望当值为Search WithinI see时它是灰色的。使用this:Or this:True Ivo,我更改了答案以备将来参考。但问题是,如果其中有文本,颜色也会更改。我希望颜色表现得像WebKit中的HTML5占位符,您可以显式设置文本的颜色。如果它仍然继承文本框的颜色,请放置一个!在规则末尾很重要。另外,没有:blur伪类(至少我没有找到文档)。对于模糊的情况,只需使用“#additionalsearch”,并让:focus情况覆盖它。但是现在如果用户输入关键字。然后模糊文本框,颜色变为灰色。我只希望当值为Search WithinI see时它是灰色的。使用this:Or this:True Ivo,我更改了答案以供将来参考。我想他的目标是使用一种比将JavaScript内联到HTML中更优雅的解决方案。CSS将解决颜色的问题,默认值仍然需要JavaScript。“…但我想知道如何设置CSS。”我看不到这一点,但正如我所说的,这是更好的。也许我看错了。@Ivo这实际上是我在寻找的,因为当框中有文本时,你的解决方案会改变文本的颜色。我想他的目标是使用一种更优雅的解决方案,而不是将JavaScript内联到HTML中。CSS将解决颜色的问题,默认值仍然需要JavaScript。“…但我想知道如何设置CSS。”我看不到这一点,但正如我所说的,这是更好的。也许我看错了。@Ivo这实际上就是我要找的,因为当框中有文本时,你的解决方案会改变文本的颜色。