Get ID from this.parent

99封情书 提交于 2019-12-11 04:59:29

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!