我最近一直在使用地理定位和HTML,我想在按下提交按钮时尝试显示iframe,我一直在坚持做什么。


 <!DOCTYPE html>
< html>
< body>

< input type =“text”id =“text”/>& #xA;< input type =“button”id =“btn”value =“Submit”onClick =“javascript:window.open('https://api.ipdata.co/'+ document.getElementById('text' )。值);” />

< / body>
< / html>



 哦和这是我的iframe
 
 IPadress是我正在测试它的设备的ipadress。我想把它放在“javascript:window.open”是

答案 0 :(得分:0)
我不确定您为什么要显示带有JSON响应的iframe ...
var text = document.getElementById("text"),
btn = document.getElementById("btn"),
iframe = document.getElementById("iframe");
function text2iframe() {
iframe.src = "https://api.ipdata.co/" + text.value;
}
btn.addEventListener("click", text2iframe);

<input type="text" id="text" value="47.91.202.22"><br>
<input type="button" id="btn" value="Submit"><br>
<iframe id="iframe"></iframe>
&#13;
什么时候可以直接使用JSON!
var text = document.getElementById("text"),
btn = document.getElementById("btn"),
pre = document.getElementById("pre");
function ipdata() {
var request = new XMLHttpRequest();
request.open('GET', "https://api.ipdata.co/" + text.value, true);
request.addEventListener("load", function() {
if (request.status >= 200 && request.status < 400) {
pre.textContent = request.responseText
var responseObj = JSON.parse(request.responseText);
console.log( responseObj.country_name )
console.dir( responseObj );
} else {
// error
}
});
request.addEventListener("error", function() {
// connection error
});
request.send();
}
btn.addEventListener("click", ipdata);
&#13;
<input type="text" id="text" value="47.91.202.22"><br>
<input type="button" id="btn" value="Submit"><br>
<pre id="pre"></pre>
&#13;