如何通过检查是否有类'拖动圈来获得zoneID-37?我可以通过
选择元素$('.drag-circle')
但接着是什么?
<div class="circle circleID-0 zoneID-37 drag-circle" style="top: 201px; left: 382.5px;"></div>
答案 0 :(得分:4)
var classes = $('.drag-circle').get(0).className;
var numb = classes.match(/zoneID-(\d+)/);
var klass = numb ? numb[0] : null; // zoneID-37
var number = numb ? numb[1] : null; // 37
答案 1 :(得分:1)
您可以使用attr()函数
获取所有类$('.drag-circle').each(function(){
var classes = $(this).attr('class').split(" ");
//Your code here to check a class in array of classes
})
但我宁愿建议您将值存储在data属性中。防爆。 HTML
<div class="circle circleID-0 zoneID-37 drag-circle" data-zoneID="37" style="top: 201px; left: 382.5px;"></div>
并通过
访问它$('.drag-circle[data-zoneID]')
或
的特定区域$('.drag-circle[data-zoneID="37"]')
答案 2 :(得分:1)
使用属性选择器检查类是否包含(使用'〜=')您的值:
if ($('.drag-circle[class~="zoneID-37"]')) {
// Your code here
}
编辑:添加JSFiddle以演示
答案 3 :(得分:0)
你可以获得所有课程的列表,然后检查它是否有该课程。 jquery的
var classList =$('div').attr('class').split(/\s+/);
$.each( classList, function(index, item){
if(item === 'drag-circle')
{
alert(classList[2]);
}
});
答案 4 :(得分:0)
OMG这个问题可以通过css轻松解决,不需要js中的代码行
$(".drag-circle.zoneID-37")