jQuery手风琴()无效

时间:2013-04-02 23:23:15

标签: javascript jquery html5 jquery-ui

我的jQuery手风琴()没有处理我的HTML段落。我哪里出错了?

simple.html

<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <div id="mainContent">
            <h1>This is an According Example</h1>           
        </div>
        <div id="accordion">
            <h3><a href="#">Heading for first sentence</a></h3>
            <div>
            <p>This is the first sentence.</p>
            </div>          
            <h3><a href="#">Heading for second sentence</a></h3>            
            <div>
            <p>This is the second sentence.</p>
            </div>
            <h3><a href="#">Heading for third sentence</a></h3>
            <div>
            <p>This is the third sentence.</p>
            </div>
        </div>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
        <script src="myscript.js"></script>
    </body>
</html>

myscript.js

window.onload = function(){
    $("#accordion").accordion();
};

3 个答案:

答案 0 :(得分:2)

两个问题:您没有包含jQuery UI CSS,您的脚本也必须包含在您的<head>中。使用此:

<head>
  <title>My Title</title>
  <link href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css">
  <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script type="text/javascript" src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
  <script>$(document).ready(function(){ $( "#accordion" ).accordion(); });</script>
</head>

并从页面底部删除脚本。

小提琴:http://jsfiddle.net/cxJW6/(这并不重要,因为您的问题在于在页面中包含jQuery + jQuery UI)

答案 1 :(得分:2)

使用此...

<!DOCTYPE html>
<html>
    <head>
    <title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
    <script src="myscript.js"></script>
</head>
<body>
    <div id="mainContent">
        <h1>This is an According Example</h1>           
    </div>
    <div id="accordion">
        <h3><a href="#">Heading for first sentence</a></h3>
        <div>
        <p>This is the first sentence.</p>
        </div>          
        <h3><a href="#">Heading for second sentence</a></h3>            
        <div>
        <p>This is the second sentence.</p>
        </div>
        <h3><a href="#">Heading for third sentence</a></h3>
        <div>
        <p>This is the third sentence.</p>
        </div>
    </div>
</body>

并将其放在头标记中

<script>
    $(document).on('ready', function(){
        $("#accordion").accordion();
    });
</script>

并查看此jsFiddle example

答案 2 :(得分:0)

$(document).ready(function() {
      $("#accordion").accordion();
});