如何在 jQuery 的手风琴事件中获取元素
How to get element within accordion event in jQuery
我正在使用以下脚本来检测点击事件,因此手风琴项会展开,我需要获得具有 class .cellArrow
.
的 div
Html
<div class="cellArrow"></div>
JS
$("#accordionContainer").on("accordionbeforeactivate", function (event, ui) {
var cellArrow = $(this).find(".cellArrow");
};
问题是手风琴的每个部分都有这个 class 所以这里我有所有 .cellArrow
classes.
我的问题是如何在触发手风琴元素中准确获取 <div>
?
谢谢!
P.S。这是 HTML 我有:
<div id="accordionContainer">
<h3>Item 1
<div class="cellArrow"></div>
</h3>
<div>First content panel</div>
<h3>Item 2
<div class="cellArrow"></div>
</h3>
<div>Second content panel</div>
</div>
试试这个代码,
$(function() {
$("#accordionContainer").accordion();
$("#accordionContainer").on("accordionbeforeactivate", function(event, ui) {
var cellArrow = $('> .cellArrow',ui.newHeader);
alert(cellArrow.html());
});
})
<link href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" type="text/css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js" type="text/javascript"></script>
<div id="accordionContainer">
<h3>Item 1
<div class="cellArrow">1</div>
</h3>
<div>First content panel</div>
<h3>Item 2
<div class="cellArrow">2</div>
</h3>
<div>Second content panel</div>
</div>
您的选择器应该是:
var cellArrow = $(ui.newHeader).find('.cellArrow');
我正在使用以下脚本来检测点击事件,因此手风琴项会展开,我需要获得具有 class .cellArrow
.
div
Html
<div class="cellArrow"></div>
JS
$("#accordionContainer").on("accordionbeforeactivate", function (event, ui) {
var cellArrow = $(this).find(".cellArrow");
};
问题是手风琴的每个部分都有这个 class 所以这里我有所有 .cellArrow
classes.
我的问题是如何在触发手风琴元素中准确获取 <div>
?
谢谢!
P.S。这是 HTML 我有:
<div id="accordionContainer">
<h3>Item 1
<div class="cellArrow"></div>
</h3>
<div>First content panel</div>
<h3>Item 2
<div class="cellArrow"></div>
</h3>
<div>Second content panel</div>
</div>
试试这个代码,
$(function() {
$("#accordionContainer").accordion();
$("#accordionContainer").on("accordionbeforeactivate", function(event, ui) {
var cellArrow = $('> .cellArrow',ui.newHeader);
alert(cellArrow.html());
});
})
<link href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" type="text/css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js" type="text/javascript"></script>
<div id="accordionContainer">
<h3>Item 1
<div class="cellArrow">1</div>
</h3>
<div>First content panel</div>
<h3>Item 2
<div class="cellArrow">2</div>
</h3>
<div>Second content panel</div>
</div>
您的选择器应该是:
var cellArrow = $(ui.newHeader).find('.cellArrow');