jQuery accordion() not working

jQuery accordion() not working

本文关键字:not working accordion jQuery      更新时间:2023-09-26

我的jQuery accordion()不能在我的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();
};

用这个…

<!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>

把这个放到head标签

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

看jsFiddle的例子

两个问题:你不包括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>

并从页面底部删除脚本。

Fiddle: http://jsfiddle.net/cxJW6/(这并不意味着太多,因为你的问题是包括jQuery+jQuery UI在你的页面)

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