1

我的 jQuery 手风琴()不能处理我的 HTML 段落。我哪里做错了?

简单的.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();
};
4

3 回答 3

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)

于 2013-04-02T23:29:47.860 回答
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>

并将其放在 head 标签中

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

看看这个jsFiddle 例子

于 2013-04-02T23:31:57.790 回答
0
$(document).ready(function() {
      $("#accordion").accordion();
});
于 2013-04-02T23:31:47.923 回答