jquery如何获取父级的名称

Thu*_*rlo 8 javascript jquery

即时搜索提供父母姓名的元素.这个:

            $('.Item').click(function(){
                var a = $(this).parent();
                alert(a[0].tagName);
            });
Run Code Online (Sandbox Code Playgroud)

只是说"DIV",但我需要一个元素的真名.谢谢

Mar*_*ein 11

请尝试以下操作(警告标记名称,然后是真实姓名):

我用了 $(a[0]).attr('name');

例如

$('.Item').click(function() {
        var a = $(this).parent();
        alert(a[0].nodeName.toLowerCase()); //Tag Name

        alert($(a[0]).attr('name')); //Attribute (real) name
        //OR
        alert(a.attr('name')); //Attribute (real) name
    });
Run Code Online (Sandbox Code Playgroud)

  • 使用a.attr('name')代替$(a [0]).attr('name') (3认同)