如何查询并保存link到数组中以便以后调用?
How to query and save link in array to call later?
我正在维基百科中查询以获取带有 link 的片段和标题。然后,如果我单击标题,我想以模式获取完整文章。
我正在尝试为每个不同的文章获取不同的文章 link 我在第一个查询中得到。
$("#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("<h3>Results for <b>" + articleName + "</b></h3>").text();
$.each(data.query.search, function(i, item) {
$("#results").append("<li><a href='http://it.wikipedia.org/wiki/" + encodeURIComponent(item.title) + "' data-toggle='modal' data-target='.bs-example-modal-lg'>" + item.title + "</a><br>" + 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
<button id="wiki" data-subject="Paris">Wikipedia</button>
<output id="results">
<ul>
</ul>
</output>
<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
</div>
</div>
</div>
在#wiki
元素click
、event.preventDefault()
、event.stopPropagation()
在ul a
元素click
事件中使用.one()
;在 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("<h3>Results for <b>" + articleName + "</b></h3>").text();
//For each result i set a <li> 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("<li><a href='https://en.wikipedia.org/wiki/" + encodeURIComponent(item.title) + "' data-toggle='modal' data-target='.bs-example-modal-lg'>" + item.title + "</a><br>" + item.snippet + "</li");
// At this point I set the url in a variable
// Probably I should save the url in an array since I may have many results
var myLink = $("#results ul li a").attr("href");
// Here I disable the link
$("#results div a").attr("href", "#");
});
// Here I say that as soon as the modal is open,
//I should make another query based on the title and get the full article
// This is where I have the issue as the URL should be what I have saved
$('.modal').on('show.bs.modal', function (e) {
$.getJSON("https://en.wikipedia.org/w/api.php?action=parse&format=json&callback=?", {
page: e.relatedTarget.textContent,
prop:"text"
}, function(data) {
$(".modal-content").html(data.parse.text['*']);
});
});
});
});
jsfiddle https://jsfiddle.net/kgyc8to4/26/
我正在维基百科中查询以获取带有 link 的片段和标题。然后,如果我单击标题,我想以模式获取完整文章。
我正在尝试为每个不同的文章获取不同的文章 link 我在第一个查询中得到。
$("#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("<h3>Results for <b>" + articleName + "</b></h3>").text();
$.each(data.query.search, function(i, item) {
$("#results").append("<li><a href='http://it.wikipedia.org/wiki/" + encodeURIComponent(item.title) + "' data-toggle='modal' data-target='.bs-example-modal-lg'>" + item.title + "</a><br>" + 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
<button id="wiki" data-subject="Paris">Wikipedia</button>
<output id="results">
<ul>
</ul>
</output>
<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
</div>
</div>
</div>
在#wiki
元素click
、event.preventDefault()
、event.stopPropagation()
在ul a
元素click
事件中使用.one()
;在 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("<h3>Results for <b>" + articleName + "</b></h3>").text();
//For each result i set a <li> 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("<li><a href='https://en.wikipedia.org/wiki/" + encodeURIComponent(item.title) + "' data-toggle='modal' data-target='.bs-example-modal-lg'>" + item.title + "</a><br>" + item.snippet + "</li");
// At this point I set the url in a variable
// Probably I should save the url in an array since I may have many results
var myLink = $("#results ul li a").attr("href");
// Here I disable the link
$("#results div a").attr("href", "#");
});
// Here I say that as soon as the modal is open,
//I should make another query based on the title and get the full article
// This is where I have the issue as the URL should be what I have saved
$('.modal').on('show.bs.modal', function (e) {
$.getJSON("https://en.wikipedia.org/w/api.php?action=parse&format=json&callback=?", {
page: e.relatedTarget.textContent,
prop:"text"
}, function(data) {
$(".modal-content").html(data.parse.text['*']);
});
});
});
});
jsfiddle https://jsfiddle.net/kgyc8to4/26/