我用 3 个 div 做了 3 个不同的输入。输入中的数据从 wiki api 中提取图像。问题是如果我输入例如:input1:dog,input2:cat,input3:potato,我将得到3张与dog相同的图像,并且它会短暂显示cat和potato,但在所有3张中都相同。
很可能我迭代错误。这是代码笔:https ://codepen.io/visan90/pen/QByjKY
有人可以帮忙吗?提前谢谢各位!
<button onclick="imageWp()">Click me</button>
<input class='info' type="text">
<input class='info' type="text">
<input class='info' type="text">
<div class="viewer"></div>
<div class="viewer"></div>
<div class="viewer"></div>
function imageWp() {
let arr = [];
$(".info").each(function(x) {
arr[x] = $(this).val();
$.ajaxPrefilter(function(options) {
if (options.crossDomain && jQuery.support.cors) {
var https = window.location.protocol === "http:" ? "http:" : "https:";
options.url = https + "//cors-anywhere.herokuapp.com/" + options.url;
}
});
$.get(
"https://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text§ion=0&page=" +
arr[x] +
"&callback=?",
function(response) {
var m;
var urls = [];
var regex = /<img.*?src=\\"(.*?)\\"/gim;
while ((m = regex.exec(response))) {
urls.push(m[1]);
}
urls.forEach(function(url) {
$(".viewer")
.empty()
.append('<img src="' + window.location.protocol + url + '">');
if (i === 0) {
return false;
}
});
}
);
});
}