简体   繁体   English

jQuery addClass在页面加载时不起作用

[英]jQuery addClass not working on pageload

Hey I have a problem that is getting on my nerves and I dont get why this happens. 嘿,我遇到了一个困扰我的问题,我不明白为什么会这样。 I want to when the page loads, my h1-tag get the class .load so it fades in with a nice effect, the problem is that the addClass dont work and my h1-tag never gets the class load. 我想在页面加载时,我的h1-tag获取类.load,以便它以良好的效果淡入,问题是addClass不起作用,我的h1-tag从不加载类。 Some help please..? 请帮忙..?

HTML and jQuery in script-tag: 脚本标记中的HTML和jQuery:

<!DOCTYPE html>
<html>
<head>
    <title>New Era of Swegre</title>
    <meta charset="UTF-8"/>
    <link href='http://fonts.googleapis.com/css?family=Poiret+One' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Lato:300' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" type="text/css" href="css/home.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $('.wrapper h1').addClass('load');​
        });
    </script>
</head>
<body>
    <div class="wrapper">
        <h1>SWEGRE DESIGNS</h1>
    </div>

</body>
</html>

CSS: CSS:

body
{background-color:black}
.wrapper
    {
        position: relative;
    }
    h1 {
    opacity: 0;
    color:white;
    font-size: 21px;
    text-align: center;
    transition: opacity 1s ease-in;
}

.load {
        color:white;
    opacity: 1;

}

Replace your code with the following and it will work as you desire. 用以下代码替换您的代码,它将按您期望的方式工作。

  $(document).ready(function () { $('.wrapper h1').addClass('load'); }); 
 .wrapper { position: relative; } h1 { opacity:0; color:black; font-size: 21px; text-align: center; transition: opacity 1s ease-in; } .load { opacity: 1; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="wrapper"> <h1>SWEGRE DESIGNS</h1> </div> 

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

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