jQuery 使用jquery获取子元素的ID并存储在变量中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2905439/
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-26 14:31:37  来源:igfitidea点击:

get an id of child element and store in a variable using jquery?

jqueryattributeselement

提问by pixie54

I'm basically trying to do exactly what the subject suggests, but I'm getting "undefined" in my alert, and I'm not entirely sure why. I am fairly new to jquery, so, I probably have the syntax wrong, but not sure where to go from here. I'll post both of my attempts, which both yield "undefined" in the alert...

我基本上试图完全按照主题的建议去做,但是我在警报中变得“未定义”,我不完全确定为什么。我对 jquery 相当陌生,所以,我可能有语法错误,但不确定从哪里开始。我将发布我的两次尝试,它们都在警报中产生“未定义”......

//In my first attempt, I'm trying to get the id of the inner a tag
<ul>
                <li id="l1" class="active"><a href="#c1">Samp 1</a></li>
                <li id="l2" class=""><a href="#c2">Samp 2</a></li>
                <li id="l3" class=""><a href="#c3">Samp 3</a></li>
        </ul>

var selected = $(".active).children("a").attr("id");
    alert(selected);

//In my second attempt, I'm trying to get the id of the currently selected li
    var selected = $(".active").attr("id");
    alert(selected);

回答by Vincent Robert

$(".active").children("a").attr("id");

Your <a>elements do not have an id, only an href. And using a selector instead of the children function may make your code easier to read.

您的<a>元素没有id,只有href。使用选择器代替子函数可能会使您的代码更易于阅读。

Do you mean $(".active > a").attr("href")?

你的意思是$(".active > a").attr("href")



$(".active").attr("id");

jQuery will return the idattribute of the first element in the jQuery collection. Do you have another element with class active?

jQuery 将返回idjQuery 集合中第一个元素的属性。你有另一个带有 class 的元素active吗?

I suggest you try $("ul > li.active").attr("id")

我建议你试试 $("ul > li.active").attr("id")

回答by Nick Craver

In the first attempt, you're getting the <a>within the <li>...which doesn't have an ID, you just need this:

在第一次尝试,你得到的<a><li>...不有一个ID,你只需要这样的:

var selected = $(".active").attr("id");
alert(selected);

So your second attempt is correct, you can see it in action here.

所以你的第二次尝试是正确的,你可以在这里看到它的实际效果

If you actually meant to get the idfrom the <a>element, then you need to give them IDs and your first attempt will work, you can see it here.

如果您确实打算id<a>元素中获取,那么您需要为它们提供 ID,并且您的第一次尝试会成功,您可以在此处查看

回答by mamoo

You're getting the wrong attribute (or you have a wrong markup). There's no "id" attribute in your a tags. You have "href" attributes, so if you're trying to geth the value of "href" you should use this:

你得到了错误的属性(或者你有一个错误的标记)。您的 a 标签中没有“id”属性。你有“href”属性,所以如果你想获取“href”的值,你应该使用这个:

var selected = $(".active).children("a").attr("href");
    alert(selected);

Otherwise if you need to get the parent's id you should use:

否则,如果您需要获取父母的 id,则应使用:

var selected = $(".active).attr("id");
    alert(selected);

回答by karim79

The issue with the anchors seems to be that none of the anchors you're selecting actually have an ID. Do you mean .attr("href")by any chance?

锚点的问题似乎是您选择的锚点实际上都没有 ID。你的意思是.attr("href")有任何机会吗?