简体   繁体   English

jQuery的slideUp()/ slideDown()上的输入字段为空

[英]Input field emptied on jQuery's slideUp()/slideDown()

I have an input field with some text, and I am trying to toggle its visibility. 我有一个带有一些文本的输入字段,并且我试图切换其可见性。 The problem I'm running into is that after toggling slide animations on the field, its text is invisible. 我遇到的问题是,在现场切换幻灯片动画后,其文本不可见。 When the field is brought into focus though, the text is visible again. 但是,当该字段成为焦点时,该文本将再次可见。

I've tested this on Safari/IE/FF and the input text stays visible, but I cannot figure out why Chrome is acting so odd. 我已经在Safari / IE / FF上对此进行了测试,并且输入文本仍然可见,但是我无法弄清楚Chrome为什么如此奇怪。

http://jsfiddle.net/f63Et/1/ http://jsfiddle.net/f63Et/1/

The problem is that jQuery sets the display of the input to inline-block when it does an .slideDown() and that makes Chrome render it incorrectly. 问题在于,当jQuery执行.slideDown()时,它.slideDown()输入的显示设置为inline-block ,这会使Chrome错误地呈现它。

You have a couple of options, for example wrap the input in a div , or set the display to block 您有几个选项,例如, 将输入包装在div中 ,或将显示设置为block

您可以尝试通过放置slideDown()的回调来手动将其聚焦:

$(this).focus()

This doesn't answer the question of why Chrome behaves strangely, but, as a workaround, if you enclose your input in a div: 这不能回答为什么Chrome表现异常的问题,但是,作为一种解决方法,如果将输入内容括在div中:

<div id="text"><input type="text" /></div>

Then slideToggle the div: 然后slideToggle div:

$(document).on('click', '#slide', function() {
    $('#text').slideToggle();
});

That appears to work in Chrome. 在Chrome中似乎可以正常工作。

作为变体,您可以使用$('input').toggle('slow')

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

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