基于jQuery getJSON的赛事列表加载及选手信息展示开发需求
Got it, let's walk through building this feature step by step with clean, modern code that meets all your requirements:
1. Basic HTML Structure
First, set up the button and results container in your HTML:
<button id="fetchDataBtn">获取数据</button> <div id="getResults"></div> <div id="runnerDisplay"></div> <!-- Container to show selected race's runners -->
The extra runnerDisplay div gives us a dedicated spot to show the selected race's participants—you can adjust its placement if needed.
2. JavaScript Logic
Add this script to handle data fetching, list rendering, and click interactions:
// Grab DOM elements we'll need const fetchButton = document.getElementById('fetchDataBtn'); const resultsContainer = document.getElementById('getResults'); const runnerContainer = document.getElementById('runnerDisplay'); // Handle "获取数据" button click fetchButton.addEventListener('click', async () => { try { // Fetch race data from the specified URL const raceResponse = await fetch('http://itweb.fvtc.edu/wetzel/marathon/races/'); if (!raceResponse.ok) throw new Error('Failed to load race data'); const races = await raceResponse.json(); // Clear old results before showing new ones resultsContainer.innerHTML = ''; // Build the race list const raceList = document.createElement('ul'); races.forEach(race => { const listItem = document.createElement('li'); listItem.id = race.id; // Assign race ID as the li's id attribute (e.g., <li id="1">) listItem.textContent = race.name; // Show the race name // Add click handler to load runners for this race listItem.addEventListener('click', async () => { try { // Use the li's id to fetch the corresponding runners const runnerResponse = await fetch(`http://itweb.fvtc.edu/wetzel/marathon/races/${listItem.id}/runners`); if (!runnerResponse.ok) throw new Error('Failed to load runner data'); const runners = await runnerResponse.json(); // Render the runner names runnerContainer.innerHTML = `<h3>${race.name} 参赛选手</h3>`; const runnerList = document.createElement('ul'); runners.forEach(runner => { const runnerItem = document.createElement('li'); runnerItem.textContent = runner.name; runnerList.appendChild(runnerItem); }); runnerContainer.appendChild(runnerList); } catch (err) { runnerContainer.textContent = `加载选手数据失败: ${err.message}`; } }); raceList.appendChild(listItem); }); resultsContainer.appendChild(raceList); } catch (err) { resultsContainer.textContent = `加载赛事数据失败: ${err.message}`; } });
Key Details Breakdown:
- Async/Await for Clean Code: We use modern async/await syntax to handle asynchronous data fetching, making the code easier to read and debug.
- Race List Rendering: Each race gets an
<li>with its ID set as the element'sidattribute (exactly like your example:<li id="1">Storm King Run</li>). - Race Click Interaction: When a user clicks a race, we use the
<li>'sidto fetch the corresponding runner data. We assume the runner endpoint follows the patternraces/{raceId}/runners—adjust this URL if your actual endpoint uses a different structure. - Error Handling: Try/catch blocks ensure users see clear error messages if something goes wrong with network requests, instead of broken or missing content.
3. Optional Styling (For Better UX)
Add some basic CSS to make the interface more user-friendly:
#fetchDataBtn { padding: 8px 16px; cursor: pointer; margin-bottom: 1rem; } #getResults ul { list-style: none; padding: 0; } #getResults li { padding: 8px; border: 1px solid #eee; margin-bottom: 4px; cursor: pointer; } #getResults li:hover { background-color: #f5f5f5; } #runnerDisplay { margin-top: 1rem; padding: 1rem; border-top: 1px solid #ddd; }
This implementation covers every requirement you outlined: fetching race data on button click, displaying the list with proper ID attributes, and loading runner details using the race ID from the <li> element.
内容的提问来源于stack exchange,提问作者Allison Gandara

