开发者

How to get parent id of an anchor tag using prototype?

开发者 https://www.devze.com 2023-01-08 05:38 出处:网络
Event.observe(window, \'load\', function() { $$(\'a.tag_links\').each(function(s) { //alert(s.parent.parent.id); //How to get id of its parent to parent
Event.observe(window, 'load',
function() { 

    $$('a.tag_links').each(function(s) {
        //alert(s.parent.parent.id); //How to get id of its parent to parent
    });
}
);

I want to get the id of the parent element.

Structure is something like this.

 <div class="home-page" id='entity-1'>
    <div class="index-pa开发者_如何学Cge-category">

    <a href="/entities/category/food" class="tag_links">food</a>

</div>

Result should be entity-1


Like this:

    $$('a.tag_links').each(function(s) {
        var parentid = $(s).up('div').id;
    });


First, your HTML is incomplete. Does it look like:

<div class="home-page" id='entity-1'>
    <div class="index-page-category"></div>

    <a href="/entities/category/food" class="tag_links">food</a>

</div>

Or like this:

<div class="home-page" id='entity-1'>
    <div class="index-page-category">

       <a href="/entities/category/food" class="tag_links">food</a>

    </div>
</div>

Either way, you should be able to achieve this by using:

$$('a.tag_links').each(function(s) {
   var divId = $(s).previous('div.home-page').id;
});

I imagine the reason you're iterating through 'a.tag_links' is that you have a bunch of links? Based on that context, is that HTML structure always consistent (the outter div will always contain a class of 'home-page')?

0

精彩评论

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