【问题标题】:jQuery accordion() not workingjQuery手风琴()不工作
【发布时间】:2013-04-02 23:23:15
【问题描述】:

我的 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();
};

【问题讨论】:

  • 我尝试了您提供的代码,尽管我在单独的脚本块中添加了 window.load 调用并且它按预期工作。正如其他人指出的那样,您应该将手风琴代码放入“文档就绪”侦听器中。它不起作用,还是看起来很奇怪 - 因为没有 CSS 文件,它看起来很奇怪,但仍然有效。
  • 您的代码可以正常工作 - jsfiddle.net/twgjW 。什么是“不工作”?首选使用$(document).ready,因为它不会等待加载图像等外部资源。它不会有那么大的不同,但你最好保持一致,因为你使用的是 jQuery。
  • +1 感谢您的回复。当我点击链接(例如第一句的标题)时,标题下方的句子不会消失或重新出现。

标签: javascript jquery html jquery-ui


【解决方案1】:

使用这个...

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

【讨论】:

    【解决方案2】:

    两个问题:你没有包含 jQuery UI CSS,你的脚本也必须包含在你的 &lt;head&gt; 中。使用这个:

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

    【讨论】:

    • 投反对票的用户能否告诉我原因?
    【解决方案3】:
    $(document).ready(function() {
          $("#accordion").accordion();
    });
    

    【讨论】:

      猜你喜欢
      • 2012-10-07
      • 1970-01-01
      • 2011-05-30
      • 2016-06-22
      • 1970-01-01
      • 2016-09-02
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多