开发者

jquery how to get Name of parent

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

            $('.Item').click(function(){
                var a = $(this).parent();开发者_如何学C
                alert(a[0].tagName);
            });

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


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


Try:

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


Use:

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


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

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


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.


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 


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜