jquery how to get Name of parent

余生长醉 提交于 2019-12-20 15:08:55

问题


im searching for the Element whicht Provides the Name of a Parent. This:

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

just says "DIV", but I need the real name of a Element. Thanks


回答1:


Try the following (Alerts the tag name, and then the Real Name) :

I used $(a[0]).attr('name');

e.g.

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



回答2:


Try:

var a = $(this).parent().attr('name');



回答3:


Use:

$('.Item').click(function(){
            var a = $(this).parent();
            alert(a.attr('name'));
        });



回答4:


look at this: http://jsfiddle.net/R833t/1/

$('.Item').click(function(){
                var a = $(this).parent();
                alert(a.attr('name'));
            });



回答5:


You could just use plain Javascript for this - in this case it's even simpler (I think):

$('.Item').click(function() {
    var parent = this.parentElement;
    var tagName = parent.tagName; // tag name, like 'div', 'a', etc...
    var name = parent.name // value of the 'name' attribute
});

My source: JavaScript DOM. Take a look at it, and help yourself.

Edit: wrapped code inside the click event handler.




回答6:


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 getting issue on this code 



回答7:


if you want to get the attribute "name" you should use .attr() $('.Item').click(function(){ var a = $(this).parent(); alert(a[0].attr("name")); });



来源:https://stackoverflow.com/questions/6840291/jquery-how-to-get-name-of-parent

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