简体   繁体   English

检查jquery中3个元素的行中元素是第一个还是第三个

[英]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.

相关问题 使用jQuery检查某个元素是否是另一个元素中的第一件事 - Check, with jQuery, if an element is the first thing in another element 使用jQuery为每个第一,第二和第三个元素提供一个唯一的类 - Give every first, second and third element a unique class using jQuery jQuery:测试if元素是MATCHED元素的第一个 - jQuery: test if element is first of MATCHED elements 获取两个输入元素的值,然后检查第三个值。 将值设置为第四元素 - Get the values of two input elements and check the value ot third. Set value to forth element 使用javascript将第一个元素之后的所有元素添加到div中(无jQuery) - Add all elements after first element to a div by using javascript (No jQuery) jQuery过滤元素,如果不存在类且它是第一个元素,则添加类 - jQuery filter elements and add class if class not present and if it's the first element 在jQuery中,如何测试某个元素是否是同一类的许多元素中的第一个? - In jQuery, how to test whether an element is the first of many elements of same class? 显示父元素列表中的每个第一个元素(jQuery + CSS) - Show each first element in a list of parent elements (jQuery+CSS) 如何使用jQuery检查另一个元素/标签内是否没有元素/标签 - How to check if there are no elements/tags inside another element/tag with jQuery jQuery回调函数,可在单击元素时检查子元素的数量 - jQuery callback function to check number of child elements on element click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM