met*_*rsk 12 javascript jquery
这是我的代码:
var textArray = ['#text1', '#text2', '#text3', '#text4',
'#text5', '#text6', '#text7', '#text8']
$('#capture').click(function() {
for (var i in textArray) {
console.log($(i).offset());
}
});
Run Code Online (Sandbox Code Playgroud)
不知道为什么我在控制台中未定义.我觉得我错过了很简单的事情.
Ry-*_*Ry- 12
一个for…in在JavaScript中的循环遍历一个对象的关键点,而不是它的值.你可以使用Array.prototype.forEach,给予支持; $.each因为你正在使用jQuery,所以也可以作为后备.
var textArray = ['#text1', '#text2', '#text3', '#text4',
'#text5', '#text6', '#text7', '#text8'];
$('#capture').click(function() {
textArray.forEach(function (x) {
console.log($(x).offset());
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
445 次 |
| 最近记录: |