如何查询并保存数组中的链接以便以后调用?
我正在维基百科中进行查询,以获取一个片段和带有链接的标题。 然后,如果我点击一个标题,我想在一个模态中获得完整的文章。
我试图为第一个查询中的每个不同链接获取不同的文章。
这是一个JsFiddle
$("#wiki").on('click', function(e) { var articleName = $(this).data('subject'); $.getJSON("https://it.wikipedia.org/w/api.php?callback=?", { srsearch: articleName, action: "query", list: "search", format: "json" }, function(data) { $("#results ul").empty(); $("#results ul").append("Results for " + articleName + "
").text(); $.each(data.query.search, function(i, item) { $("#results").append("" + item.title + "
" + item.snippet + "</li"); var myLink = $("#results ul li a").attr("href"); $("#results div a").attr("href", "#"); $('.modal').on('show.bs.modal', function (e) { $.getJSON("https://it.wikipedia.org/w/api.php?action=parse&format=json&callback=?", { page: articleName, prop:"text" }, function(data) { $(".modal-content").html(data.parse.text['*']); }); }); }); });
HTML
在#wiki
元素中使用.one()
click
, event.preventDefault()
, event.stopPropagation()
在ul a
元素click
事件; 在show.bs.modal
事件处理程序中的$.getJSON()
调用中将page
设置为e.relatedTarget.textContent
$("ul").on("click", "a", function(e) { e.preventDefault(); e.stopPropagation(); }) $("#wiki").one('click', function(e) { var articleName = $(this).data('subject'); $.getJSON("https://en.wikipedia.org/w/api.php?callback=?", { srsearch: articleName, action: "query", list: "search", format: "json" }, function(data) { $("#results ul").empty(); $("#results ul").append("Results for " + articleName + "
").text(); //For each result i set a with the title and snippet. //The title will be a disabled link with the //data-attributes to load the modal $.each(data.query.search, function(i, item) { $("#results").append(" " + item.title + "
" + item.snippet + "
jsfiddle https://jsfiddle.net/kgyc8to4/26/