我有以下标记:
<div id="div-1">1st div</div>
<div id="div-2">2nd div</div>
<div id="div-3">3rd div</div>
<div id="div-4">4th div</div>
<div id="div-5">5st div</div>如何测试5st div是否是页面上id为div-%s的最后一个div?
发布于 2012-02-28 19:54:50
你可以这样做:
var lastDivId = $('div:last').attr('id');
if(lastDivId === 'div-5'){
//the last div is the one with id = div-5编辑-甚至更好(在这种情况下,您只考虑使用以div-开头的id的div
var lastDivId = $('div[id^=div-]:last').attr('id');
if(lastDivId === 'div-5'){
//the last div is the one with id = div-5发布于 2012-02-28 19:57:11
效率不是很高,但至少它应该可以工作。
var i = 0,
el;
do {
el = document.getElementById( 'div-' + i );
i += 1;
} while( el );
// now el contains a reference to the last div element with an id of your syntax发布于 2012-02-28 20:00:14
尝尝这个。它将选择id中包含文本div-的最后一个div,并告诉您其id。只是
if ($('div[id^=div-]:last').attr('id')=='div-5'){
alert('div 5 is the last div');
else
alert('div 5 is NOT the last div');https://stackoverflow.com/questions/9481386
复制相似问题