简体   繁体   English

将val()与jquery一起使用//与jquery一起消失的输入文本

[英]using val() with jquery // input text dissappear with jquery

I have an input box, where i would like a text to disappear. 我有一个输入框,我想要一个文本消失。 After googling around i found a nice solution over here: Fade out text value inside of text field using jQuery 谷歌搜索之后,我在这里找到了一个不错的解决方案: 使用jQuery在文本字段中淡出文本值

However, for some reason it does not work with my code over here: 但是,由于某种原因,它不适用于我的代码:

$(document).ready(function(){
  var input = $("#textboxid").val()
  input.animate({color:'white'},1000, function(){
    $(this).val('').css('color','black');
  }
});

Could you tell where i'm mistaken? 你能告诉我我错了吗? thank you 谢谢

EDIT: The text should disappear after user typed something in. 编辑:用户输入内容后,该文本应消失。

val() gets the value of the textbox, that is what's written inside it. val()获取文本框的 ,即在其中写入的内容。 It's a string. 这是一个字符串。 You can't animate a string, you can animate a jQuery object. 您不能为字符串设置动画,可以为jQuery对象设置动画。

Don't take $("#textboxid").val() , just take $("#textboxid") . 不要使用$("#textboxid").val() ,只需使用$("#textboxid")

EDIT : 编辑:

I made it simply using just CSS3. 我仅使用CSS3就做到了。 This does not require any library and is hardware accelerated. 这不需要任何库,并且已硬件加速。

 $("button").click(function() { $("input").css("color", "white"); }); 
 input { -webkit-transition: all 1s linear; transition: all 1s linear; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input value="some value"/> <button>Fade it away</button> 

There are some syntax errors in your script: 脚本中存在一些语法错误:

$(document).ready(function () {
    var input = $("#textboxid"); //you have to use the object here and not the value-string
    input.animate({
        color: '#FFF'
    }, 1000, function () {
        $(this).val('').css('color', 'black');
    }); //the bracket was missing here
});

Demo 演示版

Furthermore jQuery don't support the animation of the color-attribute. 此外,jQuery不支持颜色属性的动画。 See here for more information. 有关更多信息,请参见此处

See this demo for the animation with the included jQuery-UI Library. 有关包含的jQuery-UI库的动画,请参见此演示

Reference 参考

.animate .animate

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

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