开发者

Jquery - Get the ID of an element using find()

how can i get the id of an element by using the find method?

HTML

<div id="d1" class="line1">blib blab blub </div>

JS

$(function() {

      // hit_id = $(".line1").find("blab").attr('id');
         hit_id = $(".line1").match(/blab/).attr('id');    

         $('body').append(' - '+hit_id+' - ');
});

Working example http://www.js开发者_开发技巧fiddle.net/V9Euk/496/

Thanks in advance!

Peter


You can use :contains() for the text match, then .attr('id') like you have for the ID, like this:

 var hit_id = $(".line1:contains('blab')").attr('id');    

You can see the updated/working version here.

.match() isn't a jQuery function unless you're adding some plugin (see the console on your example page for the error: Object...has no method 'match'). :contains() will reduce the element set to those that have text containing what you pass as the string to :contains() though, which seems to be what you're after.


var hit_id = $('.line1').filter(function() {
    return $(this).text().match(/blab/);
}).attr('id');
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜