【发布时间】:2017-02-14 20:13:55
【问题描述】:
我想将我的 div 的输出更改为数组内部的 JavaScript div,例如目前我从 http://ipinfo.io 获取信息。我想在我的对话框中将其打印出来,因为正在询问并回答您位于:IP:193.945.32.21 位置:日本,东京!从您的位置获取数据。或者只是我当前的位置不必是 IP。
我的对话:{text: 'Your current location?', audio: 'music/becoming.ogg', response: input => 'You are located:' + (responce) + '!'},:
IP:193.945.32.21 地点:日本,东京
我希望它出现在我的对话框中,以便在提出问题时
你现在的位置?它将响应您位于:IP:193.945.32.21 位置:日本,东京!
let questions = [
{text:'What is your name?', audio:'music/openmind.ogg', response : input => 'Hello ' + input + '!' },
{text:'How old are you?', response : input => 'That means you were born in ' + (2017 - input) + '.'},
{text:'Where are you from?', audio:'music/beone.ogg', response: input => 'You are from ' + (input) + '.'},
{text: 'Do you eat healthy?', audio: 'music/becoming.ogg', response: input => 'Acording to my data you are eating ' + (input) + ' and that is healthy!'},
{text: 'What is your time?', audio: 'music/becoming.ogg', response: input => 'Where I am located' + (new Date().toLocaleTimeString()) + 'that is the day!'},
{text: 'What language do you speak', audio: 'music/becoming.ogg', response: input => 'Acording to me you speak: ' + language() + '!'},
{text: 'Your current location?', audio: 'music/becoming.ogg', response: input => 'You are located:' + (responce) + '!'},
];
let ipinfoResponse;
$.get("http://ipinfo.io", function (response) {
ipinfoResponse = response;
}, "jsonp");
let output = $('#output'),
input = $("#input"),
curQuestion;
function ask() {
let qi = Math.floor(Math.random() * questions.length); //depending on your needs, a check could be added if it's been asked directly before or only recycle questions when all are asked
curQuestion = questions[qi];
setOutput(curQuestion.text);
input.val('');
}
ask(); //first call
function respond(){
let q = curQuestion;
if(q.audio)
new Audio(q.audio).play();
setOutput(q.response(input.val()));
setTimeout(ask, 5000);
}
function setOutput(txt){
output.html($('<h1>').text(txt));
}
$(document).keypress(function(e) {
if (e.which == 13) {
respond();
return false;
}
});
function language () {
var userLang = navigator.language || navigator.userLanguage;
return userLang
}
$.get("http://ipinfo.io", function (response) {
$("#ip").html("IP: " + response.ip);
$("#address").html("Location: " + response.city + ", " + response.region);
$("#details").html(JSON.stringify(response, null, 4));
}, "jsonp");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<link href="css/style.css" rel="stylesheet">
<img src="http://i3.kym-cdn.com/photos/images/original/001/138/377/fcc.gif" class="img-responsive center-block" alt="ffc.gif">
<h1 class="text-center">Hello I am ZENYATTA!</h1>
<br> <br>
<div class="container">
<div class="well">
<div id="output"></div>
</div>
<div class="col-md-2 col-md-offset-5">
<div class="form-group">
<label>Responce:</label>
<input type="text" class="form-control" id="input" value="">
</div>
</div>
<div id="ip"></div>
<div id="address"></div>
</div>
<!-- jquery for enter key press -->
<script src="https://code.jquery.com/jquery-3.0.0.js" integrity="sha256-jrPLZ+8vDxt2FnE1zvZXCkCcebI/C8Dt5xyaQBjxQIo=" crossorigin="anonymous"></script>
<script src="js/s.js"> </script>
【问题讨论】:
-
你的对话在哪里?
-
@Connum 问题数组。
-
@Connum {text: '您当前的位置?',音频:'music/becoming.ogg',响应:输入 => '您位于:' + (responce) + '!'} ,
-
这没有任何意义......我真的不明白你想要实现什么......对我来说,当我执行sn-p时,我看到了我的ip和位置在输入字段下方。这不是你想要的吗?
-
@Connum 我想要的是当“您的当前位置?”字段中弹出问题时用户输入他想要的任何内容,它会在白色字段中而不是在屏幕底部以 ip 和位置作为响应。
标签: javascript html