请给我一个简单的提示在哪里挖掘!
我有多个 IP,需要在每个 IP 旁边显示位置。
我有一个数组中的 IPS 列表通过
var table = document.createElement('table');
table.innerHTML = forext;
var ips = [].slice.call(table.querySelectorAll('a[href*="?ip="]')).map(anchor => anchor.textContent).join("\n");
8.8.8.8
8.8.4.4
...
我可以通过输入框获取每个人的位置
$('.send').on('click', function(){
$.getJSON('https://ipapi.co/'+$('.ip').val()+'/json', function(data){
$('.city').text(data.city);
$('.country').text(data.country);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="ip" value="8.8.8.8">
<button class="send">Go</button>
<br><br>
<span class="city"></span>,
<span class="country"></span>
但我需要的是打印 IP 及其旁边的位置:
所以,我有这个:
8.8.8.8
8.8.8.8
但我需要这个
8.8.8.8 -Mountain View, US
8.8.8.8 -Mountain View, US
...
如何通过http://freegeoip.net/json/处理整个阵列?谢谢你。
更新 1:尝试使用:ips[i]
var ipText='Lookup...';
var table = document.createElement('table');
table.innerHTML = forext;
var ips = [].slice.call(table.querySelectorAll('a[href*="?ip="]')).map(anchor => anchor.textContent).join("\n");
var ipLocations = [];
for(i=0;i<ips.length;i++){
$.getJSON('https:/freegeoip.net/json/' + ips[i], function(data) {
// could also use data.country_name, or any other property in the returned JSON
var outputString = data.ips[i] + ' - ' + data.city + ', ' + data.country_code;
ipLocations.push(outputString);
});
}
ipText = ipLocations.join('\n');
message.innerText = ipText;