简体   繁体   English

CSS / HTML文本框消失的文本不起作用

[英]Css/Html text box dissapearing text does not work

I'm making a website . 我正在建立一个网站 When you click on my search box the text goes away, like it's supposed to, but once you click away the text does not come back and I would like it to. 当您单击我的搜索框时,文本消失了,如预期的那样,但是一旦单击,文本就不会再出现了,我希望这样做。 Here is my snippet 这是我的片段

<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>

I have alot of css behind it to make the text box enlarge and change color. 我后面有很多CSS,可以放大文本框并更改颜色。 Here is the code just in case it's relevant. 这是代码,以防万一。

#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;
}

When the box is clicked the box enlarges and the Search... text goes away like it's supposed to. 单击该框时,该框会放大,并且“ Search...文本会像预期的那样消失。 But once you click away the text does not return and I would like it to. 但是,一旦单击鼠标,文本将不会返回,我希望它返回。 How do I do that? 我怎么做? *side note sorry if website looks bad im 13 *旁注对不起,如果网站外观不好,即时通讯13

Change: 更改:

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

for: 对于:

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

Just need to change value to placeholder :) 只需更改占位符的值即可:)

If you want it to be compatible with older browsers (ones that do not support html5) you could do something like this: 如果您希望它与较旧的浏览器(不支持html5的浏览器)兼容,则可以执行以下操作:

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

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

And then set your input tag to this: 然后将input标签设置为此:

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

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM