jQuery jquery淡入不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3398882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:22:41  来源:igfitidea点击:

jquery fadeIn not working

jquery

提问by DS.

Can someone please tell me what I'm doing wrong:

有人可以告诉我我做错了什么:

style:

风格:

.warning{border: 1px solid #F0AAAA; background:#FFBABA; color: #C90000;}

markup:

标记:

 <p class="warning">A successful authorization already exists. 
                    Further authorizations are not allowed at this time.</p>

script:

脚本:

 $().ready(function () {
     alert($(".warning").html());     // WORKS
     $(".warning").fadeIn(4000);      // DOESN'T WORK
 });

回答by Nick Craver

Unless the element is hidden, no fade will occur, you need something like this:

除非元素被隐藏,否则不会发生淡入淡出,你需要这样的东西:

$(".warning").hide().fadeIn(4000);

You can give it a try here, also $()is deprecated in 1.4+, you should use $(document)or the shorter version, like this:

你可以在这里试一试$()在 1.4+ 中也不推荐使用,你应该使用$(document)或者更短的版本,像这样:

$(function() {
  $(".warning").hide().fadeIn(4000);
});

The alternative is to give the element a display: noneinitially butthis breaks for JS-disabled users, or if JavaScript errors occur preventing the fade, so you may want to steer clear of this approach.

另一种方法是为元素提供一个display: none初始值,但这对于禁用 JS 的用户来说会中断,或者如果发生 JavaScript 错误阻止淡入淡出,因此您可能希望避开这种方法。

回答by Ahmed Aman

add display:noneto your css code.

添加display:none到您的 css 代码中。

.warning{border: 1px solid #F0AAAA; background:#FFBABA; color: #C90000;display:none}

回答by Mark Schultheiss

I would tend to think you want some event to be managed for the fadin. see working example here: http://jsfiddle.net/hPHPn/

我倾向于认为您希望为时尚界管理一些事件。请参阅此处的工作示例:http: //jsfiddle.net/hPHPn/

Thus:

因此:

$(document).ready(function(){
    $(".warning").hide();// hide it initially
    $('#unhideit').click(function(){
        $(".warning").fadeIn(4000);                            });
});

for some simple markup:

对于一些简单的标记:

<p class="warning">A successful authorization already exists  
    for this Quote ID. Further authorizations are not allowed at this time.</p> 
<input type="button" id="unhideit" value="clickme" />

回答by user1880276

I have recently done the same thing in my application. At the top of my htmldocument I have:

我最近在我的应用程序中做了同样的事情。在我的html文档顶部,我有:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="advtemp3jquery.js"></script>

The part which says src="advtemp3jquery.jsrefers to my external .jsfile. I find it neater to keep the code in an external file.

所说的部分src="advtemp3jquery.js是指我的外部.js文件。我发现将代码保存在外部文件中更简洁。

The script does the following:

该脚本执行以下操作:

$(document).ready(function() {
    $('.header1,.header2').fadeIn('4000');
});