给我找一下附近旅店 (帮我找到附近的)

武汉夜店 05-12 阅读:39 评论:0

结果

JavaScript 文件 (script.js) javascript const searchForm = document.querySelector('form'); const results = document.getElementById('results');searchForm.addEventListener('submit', (e)=> {e.preventDefault();const location = e.target.querySelector('input[name="location"]').value;// 使用 API 获取附近旅馆的信息fetch(`${location}&radius=5000&type=lodging&key=YOUR_API_KEY`).then(res => res.json()).then(data => {const hotels = data.results;// 在结果列表中创建并添加旅馆信息hotels.forEach(hotel => {const li = document.createElement('li');li.innerHTML = ` 给我找一下附近旅店 (帮我找到附近的)

${hotel.name}

${hotel.vicinity}

详细信息 `;results.appendChild(li);});}).catch(err => {console.error(err);alert('抱歉,无法找到任何旅馆。');}); });style.css 文件 css body{font-family: Arial, sans-serif; }h1 {text-align: center; }form {display: flex;justify-content: center;align-items: center;margin: 20px 0; }input {padding: 5px;margin: 0 10px; }results {margin-top: 20px; }ul {list-style-type: none;display: flex;flex-direction: column; }li {margin-bottom: 10px; }a {text-decoration: none;color: blue; }
版权声明

本文仅代表作者观点,不代表武汉桑拿立场。
本文系作者授权发表,未经许可,不得转载。