jQuery 手风琴()不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15776225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:46:35  来源:igfitidea点击:

jQuery accordion() not working

javascriptjqueryhtmljquery-ui

提问by Anthony

My jQuery accordion() is not working on my HTML paragraphs. Where did I go wrong?

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

simple.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

我的脚本.js

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

回答by Mooseman

Two problems: You are not including the jQuery UI CSS, also your scripts must be included your <head>. Use this:

两个问题:您没有包含 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>

and remove the scripts from the bottom of your page.

Fiddle: http://jsfiddle.net/cxJW6/(This doesn't mean much because your problem was with including jQuery+jQuery UI in your page)

并从页面底部删除脚本。

小提琴:http: //jsfiddle.net/cxJW6/(这没有多大意义,因为您的问题是在页面中包含 jQuery+jQuery UI)

回答by MG_Bautista

Use this...

用这个...

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

And put this in the head tag

并将其放在 head 标签中

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

And see this jsFiddle example

并查看此jsFiddle 示例

回答by Thomas Buckley

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