Get ID from this.parent

2019-08-04 10:43发布

问题:

Trying to get the id name of a div using javascript when it is going by $(this).parent().

Javascript Code below:

$(function() {
        $('a.close').click(function() {
            $(this).parent().fadeOut(1500);
            $.cookie($(this).attr('id'), 'true', { expires: null});
            alert($.cookie($(this).attr('id')));
            return false;
        });
        if($.cookie('hidden') == 'true'){
            $('#errororganinfoinchide-this').hide();
        }
    });

Trying to get the id errororganinfoinchide-this from the HTML below. Trying this way as that id can vary from page to page.

        <aside class='warning' id='errororganinfoinchide-this'>
            <a href='#errororganinfoinchide-this' class='close'><img src='http://resources.site.co.nz/backgrounds/close.png' /></a>
            <img src='http://resources.site.co.nz/backgrounds/icon_warning.png' class='messageimg' />
            <h4>Warning - Organisation Info</h4>
            <p>Sorry, there was an error opening the "Organisation Info" Box. Please try again later.</p>
        </aside>

All help greatly appreciated. Thanks.

回答1:

Have you tried something like this:

 $(this).parent().attr('id')


回答2:

you could also do:

$(this).parent("aside.warning").attr("id");

or

$(this).parent(".warning").attr("id");