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

javascript - Using next() x number of times with jQuery - Stack Overflow

programmeradmin2浏览0评论

What's an easy way to iterate x number of times using next() (applying the same function each time)?

I am working in Sharepoint and have limited control of the HTML; what I can do is find an element by its ID, track down the closest <td>, hide() it, and then move on to the next one (I don't want all the <td>'s, just about 7 or 8 in a row).

The code below works but it's not that pretty.

$("#my-easily-identifiable-id").closest("td").hide();
$("#my-easily-identifiable-id").closest("td").next().hide();
$("#my-easily-identifiable-id").closest("td").next().next().hide();
$("#my-easily-identifiable-id").closest("td").next().next().next().hide();
[ ... etc ... ]

What's a better way to do this?

Thanks

PS: added a fiddle (genius)

What's an easy way to iterate x number of times using next() (applying the same function each time)?

I am working in Sharepoint and have limited control of the HTML; what I can do is find an element by its ID, track down the closest <td>, hide() it, and then move on to the next one (I don't want all the <td>'s, just about 7 or 8 in a row).

The code below works but it's not that pretty.

$("#my-easily-identifiable-id").closest("td").hide();
$("#my-easily-identifiable-id").closest("td").next().hide();
$("#my-easily-identifiable-id").closest("td").next().next().hide();
$("#my-easily-identifiable-id").closest("td").next().next().next().hide();
[ ... etc ... ]

What's a better way to do this?

Thanks

PS: added a fiddle (genius)

Share Improve this question edited Jun 11, 2023 at 12:00 Jonas 129k101 gold badges327 silver badges405 bronze badges asked Oct 7, 2011 at 19:27 thornomadthornomad 6,78710 gold badges56 silver badges79 bronze badges 3
  • 1 Can you post some sample html that this would be working on (in a fiddle)? – Michael Haren Commented Oct 7, 2011 at 19:30
  • check out api.jquery./nextUntil – Billy Moon Commented Oct 7, 2011 at 19:32
  • @MichaelHaren I've been away too long - I didn't even know about fiddle! That is amazing. Posted and updated my post. – thornomad Commented Oct 11, 2011 at 16:15
Add a ment  | 

4 Answers 4

Reset to default 9

Use .nextAll() + .andSelf() with .slice().

$("#my-easily-identifiable-id").closest("td").nextAll().andSelf().slice(0, 7);

I think a simpler solution than those posted so far would be .nextUntil():

//to get next 8 elements
var i = $('#my-easily-identifiable-id').index();
$('#my-easily-identifiable-id').closest('td').nextUntil('', ':lt(' + (i+8) + ')');

//to get self and next 3
var i = $('#my-easily-identifiable-id').index();
$('#my-easily-identifiable-id').closest('td').nextUntil('', ':lt(' + (i+3) + ')').andSelf();

Grabs all "next" elements until the filter is hit (in this case we choose the next 8 elements). Verified by jsFiddle.

I've not tried it, but perhaps the following might work (I'll test momentarily):

$("#my-easily-identifiable-id").siblings().slice($(this).index(),($(this).index() + 8)).hide();

Tested and verified with a JS Fiddle demo.

Maybe something like this:

$("#my-easily-identifiable-id").closest("td").hide();
$("#my-easily-identifiable-id").closest("td").nextAll().hide();
发布评论

评论列表(0)

  1. 暂无评论