2013-02-09 45 views
2

我在做一个website。当你点击我的搜索框时,文本会消失,就像它应该的那样,但是一旦你点击文本就不会回来,我希望它能够。这里是我的代码段Css/Html文本框消失文本不起作用

<div class='search'> 
<form method="get" id='search' action="http://www.google.com/search"> 
<input type="hidden" name="q" size="31" maxlength="255" value="Site Name:" /> 
<input type="text" name="q" size="31" maxlength="255" style="height: 24px;" value="Search..." onFocus="this.value=''" /> 
</div> 

我有很多的CSS的背后,使文本框放大和改变颜色。这里是代码,以防万一它是相关的。

#search input[type="text"] { 
background: url(imgs/search-white.png) no-repeat 10px 6px #444; 
border: 0 none; 
font: bold 12px Arial,Helvetica,Sans-serif; 
color: #d7d7d7; 
width:150px; 
padding: 6px 15px 6px 35px; 
-webkit-border-radius: 20px; 
-moz-border-radius: 20px; 
border-radius: 20px; 
text-shadow: 0 2px 2px rgba(0, 0, 0, 0.3); 
-webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 3px rgba(0, 0, 0, 0.2) inset; 
-moz-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 3px rgba(0, 0, 0, 0.2) inset; 
box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 3px rgba(0, 0, 0, 0.2) inset; 
-webkit-transition: all 0.7s ease 0s; 
-moz-transition: all 0.7s ease 0s; 
-o-transition: all 0.7s ease 0s; 
transition: all 0.7s ease 0s; 
outline: none; 
} 

#search input[type="text"]:focus { 
background: url(imgs/search-dark.png) no-repeat 10px 6px #fcfcfc; 
color: #6a6f75; 
width: 175px; 
-webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.9) inset; 
-moz-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.9) inset; 
box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.9) inset; 
text-shadow: 0 2px 3px rgba(0, 0, 0, 0.1); 
outline: none; 
} 
div.search 
{ 
position:fixed; 
top:8px; 
right:5px; 
} 

当盒子被点击框下放大和Search...文本消失喜欢它应该。但是,一旦你点击文本不会返回,我希望它。我怎么做? *侧面说明 对不起,如果网站看起来坏的IM 13

+0

顺便说一下,Java和Javascript是两种*非常*不同的语言。你想要Javascript,网页语言。 Java被用来制作* real *程序 - 这种类型是通过双击桌面上的图标来运行的。 (网络程序员没有冒犯) – MathSquared 2013-02-10 01:36:17

+0

@ MathSquared11235谢谢你,我并不知道 – 2013-02-10 01:40:57

+0

@ MathSquared11235 http://stackoverflow.com/questions/14793773/auto-google-search-results-on-my-website – 2013-02-10 01:48:26

回答

3

变化:

<input type="text" name="q" size="31" maxlength="255" style="height: 24px;" value="Search..." onFocus="this.value=''" /> 

为:

<input type="text" name="q" size="31" maxlength="255" style="height: 24px;" placeholder="Search..." onFocus="this.value=''" /> 

只需要改变的价值占位:)

+0

非常感谢你帮助了很多:) – 2013-02-09 23:51:31

+0

其实,如果我是你,我会删除'onFocus = ...'部分。占位符完成这一切。 – 2013-02-09 23:52:04

+0

不用担心:) – 2013-02-09 23:52:06

1

如果你想它与旧版浏览器(不支持html5的浏览器)兼容,你可以这样做:

function removePlaceholder(element) { 
     if (element.value == "Search...") { 
      element.value = ""; 
     } 
    } 

    function replacePlaceholder(element) { 
     if (element.value == "") { 
      element.value = "Search..."; 
     } 
    } 

然后设置你的input标签这样的:

<input type="text" name="q" size="31" maxlength="255" style="height: 24px;" value="Search..." onBlur = "replacePlaceholder(this)" onFocus="removePlaceholder(this)" /> 

希望这有助于!

+0

http://stackoverflow.com/questions/14793773/auto-google-search-results-on-my-website是我需要帮助与我alredy得到这个工作:) ty tho – 2013-02-10 01:47:25

+0

@ user1978141了解。唯一的不是所有的浏览器都支持占位符标签,所以这就是我回答这个问题的原因。 – Stephen 2013-02-10 06:52:57

+0

好吧,解释它 – 2013-02-10 07:04:52