gpt4 book ai didi

javascript - jQuery 中的 parentsUntil() 使用相反的顺序?

转载 作者:行者123 更新时间:2023-11-29 10:19:12 30 4
gpt4 key购买 nike

我有这个 html 结构:

<body>
<a>
<b>
<c>
<d>
</d>
</c>
</b>
</a>
</body>

我使用 <d>元素作为开始的第一个节点。

问题:

 var s= $("d").parentsUntil("body").andSelf().map(function(){
return this.tagName;
}).get();

它应该从底部开始到顶部意味着 s数组应该看起来像 d,c,b,a .

但它显然看起来像:["A", "B", "C", "D"]

这是为什么呢?

Jsbin

最佳答案

.andSelf() 导致 jQuery 重新排序数组。

你可以试试:

 var s= $("d").parentsUntil("body").map(function(){
return this.tagName;
}).get();

此代码的输出如下所示:["C", "B","A"]

关于javascript - jQuery 中的 parentsUntil() 使用相反的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15062670/

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