javascript - 尝试使用 jQuery 从表中提取数据
问题描述
只是为了给出问题的背景,我正在尝试从使用表格制作的 html 网站中提取数据。我设法把它们中的大部分都拉了出来,但只有一件事让我的大脑感到不安。也许我需要休息一下?
我已经将所有代码都包含在一个小提琴中,可以在这里找到。https://jsfiddle.net/ex1j6gr4/
基本上我试图从那个特定的 . 所以我在其中循环并使用某些关键字获取具有日期和作者的元素。使用 font:nth-child 是不可能的,因为并非所有标签的计数在每个页面中都不相同。(您可以在 jsfiddle 表中看到两个空的,这是一个错误)
对于日期,我制作了一系列月份名称,并且很容易通过它。
对于作者,我正在检测该元素文本的第一个单词“By”,并且它也在发挥作用。
然而,我面临的问题是当我在“.each”函数之外使用该元素时,该函数将值返回为“未定义”。这是我正在使用的 jQuery 代码。
function monthNames(string, keywords) {
return string.split(/\b/).some(Array.prototype.includes.bind(keywords));
}
var months = ["Jan.", "Feb.", "March", "April", "May", "June", "July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec."];
jQuery('td').find('font').each(function() {
var curtext = jQuery(this).text();
var has_date = monthNames(curtext, months);
if (has_date == true) {
var post_date = curtext;
jQuery('#current-date-text').html(post_date);
}
});
jQuery('#current-outside-date').html(post_date);
jQuery('td').find('font').each(function() {
var curtext = jQuery(this).text();
var i = curtext.indexOf(' ');
var first_word = curtext.substring(0, i);
if (first_word == 'By') {
var author = curtext;
var author = author.substr(author.indexOf(" ") + 1);
jQuery('#current-author-text').html(author);
}
});
jQuery('#current-outside-author').html(author);
任何帮助将不胜感激 !
解决方案
您需要在函数之外定义变量(您有 2 个循环,第二个是试图引用在其范围之外定义的变量)。在这里,我结合了 2 个循环,删除了许多循环var
- 您只需要定义一次,然后您就可以引用实际变量。
最后,jQuery 无法找到('td')
,除非它实际上位于<table>
标签内。我没有你引用的函数,所以我放了一个小 forEach 循环来测试这个月。
jQuery(document).ready(function() {
var months = ["Jan.", "Feb.", "March", "April", "May", "June", "July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec."];
var post_date, author, curtext, has_date, first_word
jQuery('td font').each(function() {
curtext = jQuery(this).text();
has_date = false
curtext.split(" ").forEach(w => {
if (months.includes(w)) has_date = true;
})
if (has_date) {
post_date = curtext;
jQuery('#current-date-text').html(post_date);
}
jQuery('#current-outside-date').html(post_date);
curtext = jQuery(this).text();
var i = curtext.indexOf(' ');
first_word = curtext.substring(0, i);
if (first_word == 'By') {
author = curtext;
author = author.substr(author.indexOf(" ") + 1);
jQuery('#current-author-text').html(author);
}
});
jQuery('#current-outside-author').html(author);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td width="100%">
<font size="4" face="Times New Roman,Georgia,Times"><b>Some text over here</b></font>
<font size="2" face="Times New Roman,Georgia,Times"></font>
<font size="3" face="Times New Roman,Georgia,Times"><b>Some random text here again</b></font>
<font size="2" face="Times New Roman,Georgia,Times"></font>
<font size="3" face="Times New Roman,Georgia,Times">July 16, 2001</font>
<font size="3" face="Times New Roman,Georgia,Times">By Author name</font>
</td>
</tr>
</table>
<p id="current-date-text"></p>
<p id="current-outside-date"></p>
<p id="current-author-text"></p>
<p id="current-outside-author"></p>
推荐阅读
- amazon-web-services - 尽管通过带有 EIP 的 NAT 网关进行路由,AWS Lambda 仍没有接收到静态 IP
- sql - SSIS 2017“执行 SQL 任务”失败找不到存储过程
- javascript - 从另一个页面访问功能(IE11)
- generics - 如何避免 Rust 中重复的长泛型约束
- android - 我想将 google 移动广告依赖项添加到我的 build.gradle 文件中。但它显示了一个红色标记的下划线,这对我的应用程序发生运行时崩溃
- c# - Picasso 未从 ASP.NET Core Web API 服务器上的 URL 加载图像
- sql - SQL中的递归连接
- c - 为什么这个 djb2 实现循环终止?
- python - 用 Pandas 重塑 csv:加入 df 的两个子集
- python-3.x - 如何从递归函数返回值而不是打印到控制台?