我试图检查切换元素是否有特定的类,我不知道问题可能来自哪里:
<div class="tog">
<div id="1" class="toggle toggle-light on"></div>
</div>
<div class="tog">
<div id="2" class="toggle toggle-light on"></div>
</div>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
并在js:
$(document).ready(function() {
$(".myButton" ).on("click",function(){
$('.tog').each(function(i, obj) {
var s = "yes";
if ($(this).children("div").hasClass('toggle-off')) {
s = "no";
}
console.log($(this).children("div").attr('class'));
console.log(obj.id+s);
});
});
结果(在Firebug控制台中):
toggle toggle-light on
1 yes
toggle toggle-light on
2 yes
但如果我现在关闭切换按钮:
我根本不明白......任何想法?
答案 0 :(得分:0)
我终于找到了答案:
$(".myButton" ).on("click",function(){
$('.toggle').each(function(i, obj) {
console.log($(this).data('toggles').active);
//will show true or false depending the toggle is active or not
});
});