[英]Check if element is first or third in rows of 3 elements in jquery
If i have multiple rows of DIVs and each row has 3 elements, how to check if the element is the first in the row or the third in the row? 如果我有多行DIV并且每行有3个元素,如何检查元素是行中的第一行还是行中的第三行? For example:
例如:
<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>
So how to check if element is 1,4,7,10 or if it is 3,6,9,12 using jquery or javascript? 那么如何使用jquery或javascript检查元素是否为1,4,7,10或者它是3,6,9,12?
Thanks 谢谢
Using container's class=".cont", the following will alert "First or Third" on div click: 使用容器的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');
});
Use modulo: 使用模数:
http://jsfiddle.net/fQdHk/1/ http://jsfiddle.net/fQdHk/1/
(function () {
var n = 3;
$('div').each(function (i) {
if(i % n === 0) $(this).css({clear:'left'});
});
})();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.