【发布时间】:2017-08-03 22:33:52
【问题描述】:
请给我一个简单的提示在哪里挖掘!
我有多个 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;
【问题讨论】:
-
您是否尝试过使用
$('.ip').val()+' - '+data.city或只是猜测但data.ip如果它包含在json 中...... 示例:$('.city').text(data.ip+" - "+data.city);或者创建另一个跨度并将data.ip+" - "附加到那个... -
“打印 IP”是什么意思?您是指将它们插入 HTML,还是将它们记录在浏览器控制台中?
-
无论哪种方式,听起来您都想研究 JavaScript for...of loops。如果您在将结果插入页面内容时遇到问题,您应该查看 jQuery 文档以了解如何创建和插入新元素,并在循环中执行此操作
-
你需要什么“继续整个数组”?阵列将来自哪里?几个文本输入字段? '.ip' 文本字段,用逗号分隔?如果您想在现有 IP 旁边添加 IP,只需在“.city”跨度之前添加一个跨度,例如
<span class="theIP"></span> -并在您的 onclick 函数中添加$('.theIP').text(data.ip); after the $('.country')...行 -
@JessicaRay 啊,所以你有多个 IP ......你不认为从一开始就在你的问题中说这个是相关的吗?您还添加了更多相关的源代码......还有什么与您的问题相关的吗?如果是这样,那么我建议您添加它而不是让人们猜测。
标签: javascript geolocation freegeoip