gpt4 book ai didi

javascript - 为什么在包含字符串的数组上运行 jquery each() 会将每个字符串分解为单个字符的数组?

转载 作者:行者123 更新时间:2023-11-29 19:15:37 26 4
gpt4 key购买 nike

我已经加载了 jquery,并且我有一个包含如下字符串的数组(对象):

window.stack = [
"header",
"nav",
".content",
"footer"
];

当我使用 jquery 的 each() 函数通过循环运行它并尝试像这样再次取回我的每个字符串时:

$.each(window.stack,function(){
var h = this;
console.log(h);
})

...我明白了:

String [ "h", "e", "a", "d", "e", "r" ]
String [ "n", "a", "v" ]
String [ ".", "c", "o", "n", "t", "e", "n", "t" ]
String [ "f", "o", "o", "t", "e", "r" ]

为什么我不直接得到:

header
nav
.content
footer

?

最佳答案

您可以使用$.each(window.stack, function(key, value)

来自 http://api.jquery.com/jquery.each/

The value can also be accessed through the this keyword, but Javascript will always wrap the this value as an Object even if it is a simple string or number value.

所以,如果你想使用this

$.each(window.stack,function(){
var h = this;
console.log(h.toString());
})

关于javascript - 为什么在包含字符串的数组上运行 jquery each() 会将每个字符串分解为单个字符的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35576962/

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