jQuery: addClass to HTML5 DOM

jQuery: addClass to HTML5 DOM

本文关键字:HTML5 DOM to addClass jQuery      更新时间:2024-02-25

我今天在向HTML5DOM对象添加类时遇到问题。很抱歉,如果这是一个新手问题。:P例如:

<!DOCTYPE html>
<html>
    <head>
        <title>stackoverflow example</title>
        <style>
        .centercontent {
            width:100%;
            max-width:1080px;
            margin:0 auto;
        }
        </style>
    </head>
    <body>
        <header>
            <!-- All direct children of BODY should be centered on the page; however, children should not be added to .centercontent -->
            <h1>Site Name</h1>
        </header>
        <section>
            <p>Hello World</p>
        </section>
        <footer>
            &copy; no one 2012
        </footer>
    </body>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.j"></script>
    <script>
        $("body > *").addClass("centercontent");
    </script>
</html>

我相信这是我的JS;但是,我以前从未使用过addClass。非常感谢大家抽出时间祝你今天过得愉快!

简单编写

body > * {
    width:100%;
    max-width:1080px;
    margin:0 auto;
}

作为标题部分中的一条直接的CSS规则:查看您的示例jQuery似乎对这项任务没有真正的必要。

这个选择器在CSS块中也是有效的,它工作得很好(在IE上不是<=6)。

您的jquery中没有使用$(document).ready(function() {...});。试试这个:

$(document).ready(function() {
    $("body > *").addClass("centercontent");
});

此外,您在脚本引用的末尾缺少一个s

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.j"></script>     // original
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>    // fixed