Is there a way to embed and style a Wikipedia article in a website?

风流意气都作罢 提交于 2019-12-24 03:43:07

问题


What I want to do is basically a div-Element containing the print-friendly version of Wikipedia articles. I used an iframe to embed articles and it works, though I cannot style the document. I guess this is because the iframe content is not on my domain and I do not have editing rights. Is there a way to apply css styling to the content after the iframe is loaded?


回答1:


You can do this with AJAX by calling the wiki api with custom query string parameters.

DEMO http://jsfiddle.net/fm0vxg32/

JS

$(document).ready(function(){

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

            var markup = data.parse.text["*"];
            var blurb = $('<div></div>').html(markup);
            $('#article').html($(blurb).find('p'));

        },
        error: function (errorMessage) {
        }
    });
});

CSS

#article {
  // custom styling
}


来源:https://stackoverflow.com/questions/26577292/is-there-a-way-to-embed-and-style-a-wikipedia-article-in-a-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!