簡體   English   中英

嘗試使用 jQuery 從表中提取數據

[英]Trying to pull data from a table using jQuery

只是為了給出問題的背景,我試圖從使用表格制作的 html 網站中提取數據。 我設法把它們中的大部分都拉了出來,但只有一件事讓我的大腦感到不安。 也許我需要休息一下?

我已經將所有代碼都包含在一個小提琴中,可以在這里找到。 https://jsfiddle.net/ex1j6gr4/

基本上,我試圖從該特定內容中提取文章日期和作者。 所以我在其中循環並使用某些關鍵字獲取具有日期和作者的元素。 使用 font:nth-child 是不可能的,因為並非所有標簽的計數在每個頁面中都不相同。 (您可以在 jsfiddle 表中看到兩個空的,這是一個錯誤)

對於日期,我制作了一系列月份名稱,並且很容易通過它。

對於作者,我正在檢測該元素文本的第一個單詞“By”,並且它也在發揮作用。

然而,我面臨的問題是,當我在“.each”function 之外使用該元素時,該元素將返回值為“未定義”。 這是我正在使用的 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>標記內。 我沒有你引用的 function 所以我放了一個小 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM