得到this.parent ID(Get ID from this.parent)

2019-10-17 13:44发布

尝试使用JavaScript时,它是由$要得到一个div的ID名称(这).parent()。

下面的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-这一点。 尝试这种方式作为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>

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

Answer 1:

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

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


Answer 2:

你也可以这样做:

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

要么

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


文章来源: Get ID from this.parent