【发布时间】: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