我正在努力研究一个我找到的例子:http://jsfiddle.net/VZ3T5/1/
但是由于代码工作得很好,我可以假设我的Jqueryui 1.9.2的链接没有......
这是我尝试的代码:
<html>
<head>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link href="css/ui-lightness/jquery-ui-1.9.2.custom.css" rel="stylesheet">
<script>
var $accordion = $("#accordion");
$accordion.accordion();
$(".opener").on("click", function () {
var $this = $(this),
toOpen = $this.data("panel");
$accordion.accordion("option", "active", toOpen);
return false;
});
</script>
</head>
<body>
<a class="opener" data-panel="0" href="#">Open Section 1</a>
<a class="opener" data-panel="1" href="#">Open Section 2</a>
<a class="opener" data-panel="2" href="#">Open Section 3</a>
<a class="opener" data-panel="3" href="#">Open Section 4</a>
<div id="accordion">
<h3>Section 1</h3>
<div>
<p>Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer
ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit
amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo
ut odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.</p>
</div>
<h3>Section 2</h3>
<div>
<p>Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet
purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor
velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In suscipit
faucibus urna.</p>
</div>
<h3>Section 3</h3>
<div>
<p>Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis.
Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero
ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis lacinia
ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.</p>
<ul>
<li>List item one</li>
<li>List item two</li>
<li>List item three</li>
</ul>
</div>
<h3>Section 4</h3>
<div>
<p>Cras dictum. Pellentesque habitant morbi tristique senectus et netus et
malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus
orci luctus et ultrices posuere cubilia Curae; Aenean lacinia mauris vel
est.</p>
<p>Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus.
Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
inceptos himenaeos.</p>
</div>
</div>
</body>
</html>
提前致谢
答案 0 :(得分:2)
您的代码必须位于document.ready
试试这个
$(function () {
var $accordion = $("#accordion");
$accordion.accordion();
$(".opener").on("click", function () {
var $this = $(this),
toOpen = $this.data("panel");
$accordion.accordion("option", "active", toOpen);
return false;
});
});
答案 1 :(得分:1)
$(document).ready(function () {
var $accordion = $("#accordion");
$accordion.accordion();
$(".opener").on("click", function () {
var $this = $(this),
toOpen = $this.data("panel");
$accordion.accordion("option", "active", toOpen);
return false;
});
});
答案 2 :(得分:0)
您已将脚本包装在头部,这会导致 this
在$(document).ready()
$(document).ready(function () {
var $accordion = $("#accordion");
$accordion.accordion();
$(".opener").on("click", function () {
var $this = $(this),
toOpen = $this.data("panel");
$accordion.accordion("option", "active", toOpen);
return false;
});
});
答案 3 :(得分:0)
什么似乎不对?
尝试使用谷歌链接
// ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js
和css
// ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css
答案 4 :(得分:0)
在访问元素之前,DOM必须准备就绪。
这样做:
$(function () {
var $accordion = $("#accordion");
$accordion.accordion();
$(".opener").on("click", function () {
var $this = $(this),
toOpen = $this.data("panel");
$accordion.accordion("option", "active", toOpen);
return false;
});
});
另请注意:
$(function(){
});
是$(document).ready(function(){});