问题
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");
来源:https://stackoverflow.com/questions/14313148/get-id-from-this-parent