[英]Check, with jQuery, if an element is the first thing in another element
[英]Check if element is first or third in rows of 3 elements in jquery
如果我有多行DIV并且每行有3个元素,如何检查元素是行中的第一行还是行中的第三行? 例如:
<div>1</div> <div>2</div> <div>3</div>
<div>4</div> <div>5</div> <div>6</div>
<div>7</div> <div>8</div> <div>9</div>
<div>10</div> <div>11</div> <div>12</div>
那么如何使用jquery或javascript检查元素是否为1,4,7,10或者它是3,6,9,12?
谢谢
使用容器的class =“。cont”,以下内容将在div点击时提醒“First或Third”:
$('.cont div').click(function(){
var divIndex = $(this).index('.cont div');
if(((divIndex % 3) == 2) || ((divIndex % 3) == 0))
alert('First or Third');
});
使用模数:
(function () {
var n = 3;
$('div').each(function (i) {
if(i % n === 0) $(this).css({clear:'left'});
});
})();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.