【问题标题】:Make prompt in text field disappear/reappear when user types/erases [closed]当用户键入/擦除时,使文本字段中的提示消失/重新出现[关闭]
【发布时间】:2012-05-15 13:53:44
【问题描述】:

有谁知道我怎样才能在我的搜索栏中产生这样的效果: https://www.dropbox.com/help

<input type="text" name="input">

我的意思是 onFocus 和 onBlur 效果,文本会动态消失和出现。

谢谢大家!

【问题讨论】:

  • 它使用一个绝对定位的标签——颜色在焦点上改变并且在输入被填充时被隐藏..
  • 感谢@ManseUK 我检查了他们的脚本,以便他们使用标签定位,但我需要一个例子,因为我试图实现它但我不能:/如果你能做到,我真的很感激一个关于我如何做到这一点的基本示例!
  • 我们希望您尝试自己解决此问题,而不是要求社区为您提供完整的解决方案。当您有一些代码向我们展示证明您所做的一些努力时(即使它是错误的),请更新您的问题并标记以重新打开。谢谢。
  • 不需要! @Xander 和其他好人已经帮助了我。

标签: javascript jquery html


【解决方案1】:

他们在 color 属性上使用 CSS 动画:

摘自他们的main.css:

.sick-input.focused label{ color:#ccc;transition:color .2s linear 0;
                           -webkit-transition:color .2s linear 0;
                           -moz-transition:color .2s linear 0 }

您可以使用前面提到的定义在输入字段上使用:focus 伪选择器来模拟效果。


如果由于某种原因我不够清楚:)

http://jsfiddle.net/d9CMR/


更强大的解决方案:

http://jsfiddle.net/d9CMR/3/


更新(适当的颜色变化):

http://jsfiddle.net/d9CMR/6/


来源:

HTML

<input type="text" name="input" data-default="fubar">​

CSS

input { color:#333; }    
input:focus { color:#ccc;transition:color .2s linear 0;
              -webkit-transition:color .2s linear 0;
              -moz-transition:color .2s linear 0 }
input.typing { color:#333; }​

脚本

$(function(){
    var input = $('input[name="input"]'),
        defaulttext = input.attr('data-default');

    input.val(input.attr('data-default'));

    input.on('focus', function(){
        if(input.val() != defaulttext)
            input.addClass('typing');
        else
            input.removeClass('typing');

    }).on('keydown', function(){        
        if(defaulttext == input.val()) input.val('');

        input.addClass('typing');
    }).on('blur', function(){
        if(input.val() == '') input.val(defaulttext);

        that.removeClass('typing');
    }); 
});

【讨论】:

  • 是的,谢谢!这是一个很好的观点,但是你能把它应用到我的简单例子中让我理解吗?
  • 我做了,导航到答案中的链接(jsfiddle)并单击文本字段...
  • @Xander 非常感谢!解决了。真的很感激!
  • @Andre - Stack Overflow 及其社区不是您的个人开发团队。当您甚至没有表现出任何合理的努力来自己解决这个问题时,请对像 Xander 这样的用户表示敬意。
  • @Kev 我非常感谢 Xander 并尊重他。他在解决我的需要方面帮助了我很多。如果我还在这里,试图得到答案,那是因为我尝试过自己,但失败了。
【解决方案2】:

试试这个 - 使用来自 Dropbox 网站的 HTML 和 CSS ....

HTML:

<div class="sick-input" id="anonymous_element_3">
    <label for="search-input">Type your question here</label>
    <input type="text" id="search-input" name="search_string" value="" tabindex="1">
</div>

CSS:

.sick-input label {
    font-size: 16px;
    position: absolute;
    left: 8px;
    top: 6px;
    cursor: text;
    pointer-events: none;
    color: #777;
}

.sick-input.populated label {
    display: none;
}
input {
    border-radius: 3px;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    -moz-box-shadow: 0 0 0 #000, inset 0 3px 3px #eee;
    -webkit-box-shadow: 0 0 0 #000, inset 0 3px 3px #eee;
    box-shadow: 0 0 0 #000, inset 0 3px 3px #eee;
    font-size: 16px;
    border: 1px solid #BFBFBF;
    padding: 5px;
    width: 500px;
}
.sick-input.focused label {
    color: #ccc;
    transition: color .2s linear 0;
    -webkit-transition: color .2s linear 0;
    -moz-transition: color .2s linear 0;
}

JavaScript:

$('#search-input').keyup(function() {
    if ($(this).val() === '') {
        $('.sick-input').removeClass('populated');
    } else {
        $('.sick-input').addClass('populated');
    }   
})

$('#search-input').focus(function() {
    $('.sick-input').addClass('focused');
});

$('#search-input').blur(function() {
    $('.sick-input').removeClass('focused');
});

Working example here

【讨论】:

  • 很好的例子@ManseUK。但我不知道为什么在输入单词并混合文本时会延迟..
  • @Andre 将 keydown 更改为 keyup - 为我解决了问题 :-)
  • +1 @manseuk 干得好。这是一个更好的解决方案 - 因为插入符号位于文本字段的开头。解释为什么使用标签而不是使用输入字段值...
  • @Xander 它并不完美 - 但看起来还不错......我确信它可以改进 - 只是想试一试......有些东西让我想试一试网站缩小一切!!!!
【解决方案3】:

Here 是一个例子:

HTML

<div>
    <label for="search">Search this site</label>
    <input type="text" id="search" placeholder="Search this site" />
</div>

CSS

body { padding: 20px; }

div { position: relative; }
div label { position: absolute; left: 8px; top: 4px; color: #666; z-index: 2; font: 11px arial; }
div input { position: absolute; padding: 3px 6px; border: 1px solid #ddd; border-radius: 2px; z-index: 1; font: 11px arial; }
.populated label { display: none; }

Javascript

$('input').on('keydown keypress keyup', function(e) {
    if($('input').val() == '') {
        $('div').removeClass('populated');
    }
    else {
        $('div').addClass('populated');
    }
});

如果您不想使用placeholder 属性,请使用this

HTML

<div>
    <label for="search">Search this site</label>
    <input type="text" id="search" value="Search this site" />
</div>

Javascript

$('input').on('keydown keypress keyup', function(e) {
    if($('input').val() == '') {
        $('div').removeClass('populated');
    }
    else {
        $('div').addClass('populated');
    }
}).on('focus', function(e) {
    var $this = $(this);
    if($this.val() == $this.data('placeholder')) {
        $this.val('');
    }

}).on('blur', function(e) {
    var $this = $(this);
    if($this.val() == '') {
        $this.val($this.data('placeholder'));
    }    
}).data('placeholder', $('input').val());

如果您不想使用input 字段的value 属性,那么this 可能会有所帮助:

HTML

<div>
    <label for="search">Search this site</label>
    <input type="text" id="search" value="" />
</div>

CSS

body { padding: 20px; }

div { position: relative; }
div label { position: absolute; left: 8px; top: 4px; color: #666; z-index: 2; font: 11px arial; }
div input { position: absolute; padding: 3px 6px; border: 1px solid #ddd; border-radius: 2px; z-index: 1; font: 11px arial; }
.populated label { display: none; }
.focused label { color: #aaa; }

Javascript

$('input').on('keydown keypress keyup', function(e) {
    if($('input').val() == '') {
        $('div').removeClass('populated');
    }
    else {
        $('div').addClass('populated');
    }
}).on('focus', function(e) {
    $('div').addClass('focused');
}).on('blur', function(e) {
    $('div').removeClass('focused');
});

【讨论】:

  • 不错的答案@Emre Erkan。但是只有一件事,当我在输入中输入内容时,书面文本会添加到值集中,在这种情况下是“搜索此站点”
  • 好的,我现在明白了。我更新了我的答案。 this 是你想做什么?
  • 不幸的是仍然有同样的延迟哥们..
  • 我不知道发生了什么,你的脚本可以在 jsFiddle 中运行,但是当我复制并粘贴到我的页面时却不行。你知道会发生什么吗?
【解决方案4】:

您也可以使用具有类似效果的html5 placeholder="" 属性。

并为旧版浏览器使用 jquery 后备:https://github.com/mathiasbynens/jquery-placeholder

【讨论】:

  • 类似,但不一样。一方面,在大多数浏览器(不包括 Chrome 最近的浏览器)中,占位符文本会在焦点上消失。在 Chrome 中,文本保持焦点,它不会变暗。所以你是对的:类似。
  • 是的,让我突出显示相似这个词:)
  • 谢谢大家! @allaire 我没有 html5 的经验,你可以给我一个示例脚本吗?
  • 就在这里,伙计:davidwalsh.name/html5-placeholder
  • 恭喜@allaire,但在这个例子中,文本并没有达到预期的效果。你说我可以使用 jQuery 来做到这一点,你能给我一个基本的脚本示例吗?
【解决方案5】:

你可以试试这个

Javascript 代码

function txtblur()
{
    document.getElementById('txtval').value="";
}
function textblur()
{
    document.getElementById('txtval').value="Enter word to search";
}

HTML 文本框代码

<input type="text" class="tbct" name="txtval" id="txtval" value="Enter word to search" onfocus="txtblur()"/>

【讨论】:

  • 感谢您的回复!我试过这个..没用。 (在 Chrome 浏览器中试过)
  • 该脚本适用于我的 Chrome 浏览器。你能把你试过的代码发给我吗?
【解决方案6】:

如果高度或宽度已知,您还可以将包含所需文本的背景图像精灵设置到输入字段,并且只需在 onfocus 和 onblur 事件上调整背景位置。

【讨论】:

    猜你喜欢
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多