Javascript jquery如何获取父母的名字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6840291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:24:28  来源:igfitidea点击:

jquery how to get Name of parent

javascriptjquery

提问by Thu marlo

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

只是说“DIV”,但我需要一个元素的真实名称。谢谢

回答by Marc Uberstein

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

尝试以下操作(提示标签名称,然后提示真实姓名):

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

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

回答by rolling stone

Try:

尝试:

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

回答by Darm

Use:

用:

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

回答by kleinohad

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

看看这个:http: //jsfiddle.net/R833t/1/

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

回答by Daan Wilmer

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

您可以为此使用纯 Javascript - 在这种情况下,它甚至更简单(我认为):

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

我的来源:JavaScript DOM。看看它,并帮助自己。

Edit: wrapped code inside the click event handler.

编辑:在单击事件处理程序中包装代码。

回答by Amit Kumar

var a = $(this).parent(); alert(a[0].nodeName.toLowerCase()); //Tag Name

var a = $(this).parent(); 警报(a[0].nodeName.toLowerCase());//标签名称

    alert($(a[0]).attr('name')); //Attribute (real) name
    //OR
    alert(a.attr('name')); //Attribute (real) name getting issue on this code 

回答by Neo

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

如果你想获得属性“名称”,你应该使用 .attr() $('.Item').click(function(){ var a = $(this).parent(); alert(a[0].attr("name")); });