开发者

jquery how to find a div that has a certain id?

lets say i have this:

<div id="256" class="testt1"><li>test1</li></div>
<div id="126" class="testt2"><li>test2</li></div>

how can i find the div with id 256. The thing is that i don't know those id's, they are created o开发者_C百科n the fly. So i'm thinking:

var get_it = $('div.testt1').attr('id');

but then how do i find that div if i want to remove it or something?


Once you obtain the ID just concatenate it into a selector using +:

var get_it = $('div.testt1').attr('id');
$('#' + get_it).doStuff();


try this:

$('#256') //<<-- the div with id 256

if you dont know the id make a fn for finding it:

function find_id(selector, id){

   $(selector).each(function(){
        if(this.id == id){
             return this;
        }
   })

}

//and use it like so:

var div = $(find_id('.testt1', '256')); //this is the dom element with id 256


You can't know for sure you are getting the div you want since multiple divs with the same class. This will get you the first one it finds:

$('div.testt1:eq(0)')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜