gpt4 book ai didi

javascript - 使用 jQuery 在对象集合中选择属性的多个值

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:00:50 24 4
gpt4 key购买 nike

在这个例子中,我在“LI”中选择了一堆“A”(我选择什么并不重要,只要知道我返回的是一组“A”标签,它们都具有相同的“属性结构” ).

我想知道如何返回“属性值”的逗号分隔列表(或对象/集合)。我想知道是否可以在没有循环的情况下完成。

alert($(".bzsUserSelector-selected A"));
// this returns "[object]", which is expected

alert($(".bzsUserSelector-selected A").length);
// this returns "4", which is expected for my example

alert($(".bzsUserSelector-selected A").attr("myAttribute"))
// this returns "aaa", which is the value of the FIRST "myAttribute" only, I don't want that.
// I want something like this "aaa, bbb, ccc, ddd"

我希望返回一个包含 4 个项目的对象和“myAttribute”属性的 4 个值。

我希望这已经足够清楚了。提前致谢。 - 标记

最佳答案

好吧,有很多方法可以做到这一点,但是这种特定的方法相对简洁并且利用了 makeArraymap jQuery 中的功能。

$('li').map(function() {
return $(this).attr("myAttribute")
}).get().join(',')

关于javascript - 使用 jQuery 在对象集合中选择属性的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/997430/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com