从this.parent获取ID

时间:2013-01-14 03:52:46

标签: javascript this

试图通过$(this).parent()来获取div的id名称。

以下Javascript代码:

$(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();
        }
    });

尝试从下面的HTML获取id errororganinfoinchide-this。尝试这种方式,因为该ID可能因页面而异。

        <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>

所有帮助非常感谢。感谢。

2 个答案:

答案 0 :(得分:2)

你有没有试过这样的事情:

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

答案 1 :(得分:1)

您也可以这样做:

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

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