|
|
2
dgeare
6 年前
我想您的意思是将输入的值作为参数传递到listPlanets函数中?
var planets = [
{planet: 'Mercury', position: '1', orbit_time: '0.24', nat_satellites: '0'},
{planet: 'Venus', position: '2', orbit_time: '0.62', nat_satellites: '0'},
{planet: 'Earth', position: '3', orbit_time: '1', nat_satellites: '1'},
{planet: 'Mars', position: '4', orbit_time: '1.88', nat_satellites: '2'},
{planet: 'Jupiter', position: '5', orbit_time: '11.86', nat_satellites: '67'},
{planet: 'Saturn', position: '6', orbit_time: '29.46', nat_satellites: '62'},
{planet: 'Uranus', position: '7', orbit_time: '84.32', nat_satellites: '27'},
{planet: 'Neptune', position: '8', orbit_time: '164.79', nat_satellites: '14'}
];
var listPlanets = function (i) { //i is a parameter that will be passed into the function at the time it is called
i -= 1;//the user entered a value between 1 and 8, but our array indexes are from 0 - 7. decrease input by one
document.write(planets[i].planet + ' is planet #' + planets[i].position +
' from the Sun. Time to complete its orbit is ' + planets[i].orbit_time + ' earth year(s). It has ' + planets[i].nat_satellites + ' natural satellite(s).<br>' );
}
// listPlanets();
var num = window.prompt("Please enter a number between 1 and 8");
if (1 <= num && num <= 8) {
listPlanets(num); //pass num into listPlanets function as argument
} else {
alert("The value you entered is not within range. Please reload the page and enter a value thatis within 1 and 8, inclusive.");
window.location.reload();
}
正如在一些注释中指出的,您可能更喜欢将数据反映到DOM,而不是document.write,这将覆盖现有页面,通常被认为是一种不好的做法。如果改为在DOM中执行此操作,它可能会如下所示:
var planets = [
{planet: 'Mercury', position: '1', orbit_time: '0.24', nat_satellites: '0'},
{planet: 'Venus', position: '2', orbit_time: '0.62', nat_satellites: '0'},
{planet: 'Earth', position: '3', orbit_time: '1', nat_satellites: '1'},
{planet: 'Mars', position: '4', orbit_time: '1.88', nat_satellites: '2'},
{planet: 'Jupiter', position: '5', orbit_time: '11.86', nat_satellites: '67'},
{planet: 'Saturn', position: '6', orbit_time: '29.46', nat_satellites: '62'},
{planet: 'Uranus', position: '7', orbit_time: '84.32', nat_satellites: '27'},
{planet: 'Neptune', position: '8', orbit_time: '164.79', nat_satellites: '14'}
];
var listPlanets = function (i) {
i -= 1;
document.getElementById('planet_name').innerHTML = planets[i].planet;
document.getElementById('planet_info').innerHTML = planets[i].planet + ' is planet #' + planets[i].position +
' from the Sun. Time to complete its orbit is ' + planets[i].orbit_time + ' earth year(s). It has ' + planets[i].nat_satellites + ' natural satellite(s).<br>';
}
// listPlanets();
document.getElementById('info_button').addEventListener('click', function(evt){
var num = document.getElementById('planet_input').value;
if (1 <= num && num <= 8) {
listPlanets(num);
} else {
alert("The value you entered is not within range. Please reload the page and enter a value thatis within 1 and 8, inclusive.");
}
});
Pick a planet between 1 and 8 <input id='planet_input' type='number' /><button id='info_button'>View Info</button>
<h1 id='planet_name'></h1>
<h3 id='planet_info'></h3>
|