最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if the element selectedclicked is multiple of "n" using jQuery? - Stack Overflow

programmeradmin5浏览0评论

I have 'n' li elements inside a ul. I want to alert a message only if the li selected/clicked is a multiple of "n"(let it be 3).

In the below example, alert must be shown only if I click the 3rd, 6th and 9th li element:

<ul>
  <li>list item 1</li>
  <li>list item 2</li>
  <li>list item 3</li>
  <li>list item 4</li>
  <li>list item 5</li>
  <li>list item 6</li>
  <li>list item 7</li>
  <li>list item 8</li>
  <li>list item 9</li>
</ul>

I have 'n' li elements inside a ul. I want to alert a message only if the li selected/clicked is a multiple of "n"(let it be 3).

In the below example, alert must be shown only if I click the 3rd, 6th and 9th li element:

<ul>
  <li>list item 1</li>
  <li>list item 2</li>
  <li>list item 3</li>
  <li>list item 4</li>
  <li>list item 5</li>
  <li>list item 6</li>
  <li>list item 7</li>
  <li>list item 8</li>
  <li>list item 9</li>
</ul>
Share Improve this question edited Nov 10, 2019 at 22:34 Dharman 33.5k27 gold badges101 silver badges148 bronze badges asked Nov 28, 2017 at 11:56 vishnuvishnu 6341 gold badge6 silver badges22 bronze badges 1
  • Your question says the: “alert must be shown only if I click the 3rd, 6th and 9th li,” did you mean “and” or did you mean “or”? – David Thomas Commented Nov 10, 2019 at 22:43
Add a ment  | 

5 Answers 5

Reset to default 4

Here, we can use nth-child as well.

$('ul').find('li:nth-child(3n)').click(function(){
  console.log($(this).text());
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul>
  <li>list item 1</li>
  <li>list item 2</li>
  <li>list item 3</li>
  <li>list item 4</li>
  <li>list item 5</li>
  <li>list item 6</li>
  <li>list item 7</li>
  <li>list item 8</li>
  <li>list item 9</li>
</ul>

You can use :nth-of-type() selector

$('li:nth-of-type(3n)').click(function() {
  console.log('Alert here')
})
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul>
  <li>list item 1</li>
  <li>list item 2</li>
  <li>list item 3</li>
  <li>list item 4</li>
  <li>list item 5</li>
  <li>list item 6</li>
  <li>list item 7</li>
  <li>list item 8</li>
  <li>list item 9</li>
</ul>

You could use the modulo % for this with the help of index() to get the index of the clickd li.

We should add 1 since the index is zero based :

if (($(this).index() + 1) % n == 0) {
    //Your logique here
}

Snippet:

var n = $('ul>li').length;

$('li').click(function() {
  var currentIndex = $(this).index();

  if ( (currentIndex + 1) % n == 0) 
  {
    console.log('Alert here')
  }
})
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul>
  <li>list item 1</li>
  <li>list item 2</li>
  <li>list item 3</li>
  <li>list item 4</li>
  <li>list item 5</li>
  <li>list item 6</li>
  <li>list item 7</li>
  <li>list item 8</li>
  <li>list item 9</li>
</ul>

You can try use this:

var number = 3;
$("ul li").click(function(){
  var i = $(this).index() + 1
  if(i % number == 0) {
    console.log("You clicked on either element 3,6 or 9")
  }
})

Demo

var number = 3;
$("ul li").click(function(){
  var i = $(this).index() + 1
  if(i % number == 0) {
    console.log("You clicked on either element 3,6 or 9")
  }
})
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
    <li>list item 1</li>
    <li>list item 2</li>
    <li>list item 3</li>
    <li>list item 4</li>
    <li>list item 5</li>
    <li>list item 6</li>
    <li>list item 7</li>
    <li>list item 8</li>
    <li>list item 9</li>
</ul>

Or you can use $(ul li:nth-child(3n)).click() it will take each third element.

Use jQuery index()

UPDATE : Directly running this code should satisfy your requirement. N=3 in this example.

Here is the jsfiddle.

$(document).ready(function(){
  var N = 3;
  $("ul li").click(function(){
    var index = $(this).index() + 1;
    if(index%N == 0) {
      alert("Is a multiple of "+N);
    } else {
      alert("Is not a multiple of "+N);
    }
  });
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>list item 1</li>
  <li>list item 2</li>
  <li>list item 3</li>
  <li>list item 4</li>
  <li>list item 5</li>
  <li>list item 6</li>
  <li>list item 7</li>
  <li>list item 8</li>
  <li>list item 9</li>
</ul>

发布评论

评论列表(0)

  1. 暂无评论