admin管理员组文章数量:1416073
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? 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?
Thanks
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? 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?
Thanks
Share Improve this question asked May 27, 2013 at 22:01 Michael SamuelMichael Samuel 3,92012 gold badges51 silver badges86 bronze badges 7- 1 I cannot see any "row" in your code, only indentation – A. Wolff Commented May 27, 2013 at 22:03
- And how are you positioning these elements? Will the text always reflect the element's position in the order? – David Thomas Commented May 27, 2013 at 22:04
- 3 Do you mean like this jsfiddle/mowglisanu/Rz2Qt – Musa Commented May 27, 2013 at 22:06
- I need a solution that hopefully checks mathematically this numbers like the native :odd and :even selectors but these won't work in my case. The first and third element in each row will get off screen when the resolution is low as they are absoltely position...so want to change position of these depending on user's resolution – Michael Samuel Commented May 27, 2013 at 22:08
- 1 What do you mean you want to check the element? Do you want to loop through all of these <div>s once and figure it out? Like this: jsfiddle/STMH7/1 Or do you want a function that takes in an element and will tell you its position? – skyline3000 Commented May 27, 2013 at 22:45
2 Answers
Reset to default 7Using container's class=".cont", the following will alert "First or Third" on div click:
$('.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/fQdHk/1/
(function () {
var n = 3;
$('div').each(function (i) {
if(i % n === 0) $(this).css({clear:'left'});
});
})();
本文标签: javascriptCheck if element is first or third in rows of 3 elements in jqueryStack Overflow
版权声明:本文标题:javascript - Check if element is first or third in rows of 3 elements in jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745180851a2646458.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论