【问题标题】:Css/Html text box dissapearing text does not workCss/Html 文本框消失文本不起作用
【发布时间】:2013-01-25 10:00:45
【问题描述】:

我正在制作website。当您单击我的搜索框时,文本会消失,就像它应该的那样,但是一旦您单击离开,文本就不会回来,我希望它会。这是我的sn-p

<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... 文本会按预期消失。但是,一旦您单击离开,文本就不会返回,我希望它能够返回。我怎么做? *边注 抱歉,如果网站看起来很糟糕,我 13

【问题讨论】:

  • 顺便说一下,Java 和 Javascript 是两种非常不同的语言。你想要 Javascript,一种网络语言。 Java 用于制作真正的 程序——通过双击桌面上的图标运行的那种程序。 (无意冒犯网络程序员)
  • @MathSquared11235 谢谢你我不知道

标签: java html css textbox maskedtextbox


【解决方案1】:

变化:

<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=''" />

只需要将值更改为占位符:)

【讨论】:

  • 实际上,如果我是你,我会删除 onFocus=... 部分。占位符可以做到这一切。
  • 我删除了 onFocus= part 。这个网站花了我一段时间,我一个人做,如果我不能让它工作,我就要失去理智了
  • 嘿,很快,因为这是一个谷歌搜索,我怎么能让它在你在框中输入时自动搜索一个 iframe,就像你在输入它时创建一个带有搜索结果的 iframe?我怎么能用ajax做到这一点?任何指针
  • 在此处设置一个单独的问题,它与上一个问题无关,任何其他使用此问题来帮助他们的用户可能会感到困惑:)
【解决方案2】:

如果您希望它与旧浏览器(不支持 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)" />

希望这会有所帮助!

【讨论】:

  • stackoverflow.com/questions/14793773/… 是我需要的帮助,我已经让这个工作了:) ty tho
  • @user1978141 明白了。唯一的问题是并非所有浏览器都支持占位符标签,所以这就是我回答的原因。
猜你喜欢
  • 1970-01-01
  • 2012-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
  • 2012-09-12
  • 1970-01-01
相关资源
最近更新 更多