jquery - 没有从 jQuery 循环中获取值
问题描述
我正在尝试使用 Nightmare.js 和 jquery 获取网站上元素的文本和链接。
我正在遍历具有相同类的每个元素,我没有收到任何错误,但文本和链接是空的。
这是我尝试过的:
nightmare
.goto("https://myanimelist.net/anime/season")
.wait(2000)
.evaluate(() => {
let links = [];
$('.link-title').each(() => {
item = {
"title": $(this).text(),
"link": $(this).attr("href")
};
links.push(item);
});
return links;
})
.end()
.then(result => {
console.log(result);
})
.catch(function (error) {
console.error('Failed', error);
});
控制台中的输出如下所示:
[ { title: '' },
{ title: '' },
{ title: '' },
... 99 more items
]
解决方案
$('.link-title').each(() => {
item = {
"title": $(this).text(),
"link": $(this).attr("href")
};
links.push(item);
});
在 each() 方法中,请改用普通函数。箭头功能this
无法按预期工作
推荐阅读
- python - 使用正则表达式在python中查找url
- macos - 关闭浏览器时,mac上的shinyloadtest记录不会停止
- r - For 循环没有正确过滤
- android - 迁移到 AndroidX 后已将 compileSdkVersion 更新为 28,但仍然收到有关 fontVariationSettings 和 ttcIndex 的 gradle 错误
- javascript - 我可以在不安装 Visual Studio 的情况下测试 JavaScript WinRT 代码吗?
- angular - 离子:产品建设问题
- swift - NSTableView 获取正在使用键盘编辑的单元格
- c++ - getline 在 C++ 中导致分段错误?
- android - 如何使用 volleyplus 库下载文件
- typescript - Vue 和 TypeScript:找不到名称“出口”