Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从wikipedia表中获取第一列值的列表?_Javascript_Jquery_Wikipedia_Wikipedia Api - Fatal编程技术网

Javascript 如何从wikipedia表中获取第一列值的列表?

Javascript 如何从wikipedia表中获取第一列值的列表?,javascript,jquery,wikipedia,wikipedia-api,Javascript,Jquery,Wikipedia,Wikipedia Api,我试图在第一个维基百科开始的第一列中获得年份列表,并将其放入一个选择列表中 我以这种方式阅读json,但我无法获取我需要的内容,以便将其放置在select中: $(document).ready(function(){ $.ajax({ type: "GET", url: "https://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text&section=1

我试图在第一个维基百科开始的第一列中获得年份列表,并将其放入一个选择列表中

我以这种方式阅读json,但我无法获取我需要的内容,以便将其放置在select中:

$(document).ready(function(){

    $.ajax({
        type: "GET",
        url: "https://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text&section=1&page=List_of_wars_1000%E2%80%931499&callback=?",
        contentType: "application/json; charset=utf-8",
        async: false,
        dataType: "json",
        success: function (data, textStatus, jqXHR) {

            var markup = data.parse.text["td"];
            var i = $('<div></div>').html(markup);

            // remove links as they will not work
            i.find('a').each(function() { $(this).replaceWith($(this).html()); });

            // remove any references
            i.find('sup').remove();

            // remove cite error
            i.find('.mw-ext-cite-error').remove();

            $('#article').html($(i).find('p'));         
        },
        error: function (errorMessage) {
        }
    });    

});

url的结果是一个名为*而不是td的对象,因此您的行:

data.parse.text["td"]
变成

data.parse.text["*"]
这将为您提供文章的所有标记,您已经将其解析为html。您可以使用其他URL,但这是所提供URL的结果

然后,您可以使用jquery从该文章中查找所需内容,例如:

html.find("table td:first-child") 
要从表中获取所有的第一列,您可能需要table:first用于其他文章等

工作代码段:

$document.readyfunction{ $.ajax{ 类型:GET, 网址:https://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text§ion=1&page=List_of_wars_1000%E2%80%931499&callback=?, contentType:application/json;字符集=utf-8, async:false, 数据类型:json, 成功:函数数据、textStatus、jqXHR{ var markup=data.parse.text[*]; var html=$.htmlmarkup; var cells=html.findtable td:first child; cells.eachfunction{ console.log$this.text; }; var years=cells.mapfunction{return$this.text;}.get; console.logyears.join, }, 错误:函数errorMessage{ } }; };
您似乎在响应对象中选择了错误的属性td

试一试


通过选择所有tr中的td:first子项,您可以很容易地做到这一点。如果它是另一个字段,您可以使用td:nth-child5伪选择器

这里是一个测试纯javascript的示例

var nodes = document.querySelectorAll(".wikitable tr td:first-child")
var values = Array.prototype.map.call(nodes, function(n){
    return n.innerContent;
})
与jQuery类似,您可以执行未经测试的操作

var values = $(".wikitable tr td:first-child").each(function(n){
    return n.innerContent;
})

稍后,您可以使用jQuery的wrap函数将每个文本/年份设置为一个选项元素,您可以将其传递到选择下拉列表中。以下是一个解决方案:

$document.readyfunction{ $.ajax{ 类型:GET, 网址:https://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text§ion=1&page=List_of_wars_1000%E2%80%931499&callback=?, contentType:application/json;字符集=utf-8, async:false, 数据类型:json, 成功:函数数据、textStatus、jqXHR{ var html=data.parse.text['*']; 如果!html{ 回来 } var$hiddenContent=$.htmldata.parse.text['*'].hide; var$firstColumnCells=$hiddenContent.find'table.wikitable'。find'td:first-child'; $hiddenContent.remove;//删除我们的帮助程序div var值=[]; $firstColumnCells.eachfunctionidx,单元格{ var val=$cell.text.match/\d+/[0]; 值。推送$cell.text; //您也可以在这里使用值做一些事情 $'article'.append+val+; }; //如果您喜欢,请在控制台中显示为数组,或者使用该数组执行某些操作 //console.logvalues; }, 错误:函数errorMessage{ } }; }; 第条分部{ 填充物:5px; 保证金:5px0; 背景:灰色; 宽度:自动; 颜色:白色; 宽度:100px; 文本对齐:居中; } 年
谢谢,我们可以在1083年删除字符串吗?谢谢,我们可以在1083年删除字符串吗?可能使用parseint或regex
var values = $(".wikitable tr td:first-child").each(function(n){
    return n.innerContent;
})