简体   繁体   English

使用 JS 将 HTML 中的 div 替换为 fetch() 数据

[英]Replace div in HTML with fetch() data using JS

I'm using fetch() to obtain data from a public API and have an array where I'm trying to store all job locations.我正在使用fetch()从公共 API 获取数据,并有一个数组用于存储所有工作位置。 This array is called the locations_array .该数组称为locations_array

A for loop is running to generate a .card for each key found in the JSON and in this loop, I'm running locations_array.push(job_location);一个for loop正在运行,为 JSON 中的每个key生成一个.card ,在这个循环中,我正在运行locations_array.push(job_location); to add the locations to the array.将位置添加到数组中。

In my HTML I have a div in my select where I'm looking for the markup from JS to replace:在我的 HTML 我的select中有一个div ,我正在寻找来自 JS 的标记来替换:

<select>
   <option value="">OPEN POSITIONS</option>
   <div id="location__options"></div>
</select>  

So, location__options will be replaced with the option markup I have written in my for loop JS.因此, location__options将替换为我在 for loop JS 中编写的option标记。

Full code here:完整代码在这里:

 fetch('https://boards-api.greenhouse.io/v1/boards/cutover/jobs?content=true', {}).then(function (response) { return response.json(); // response type (json) }).then(function (data) { appendDataToHTML(data); // function that appends data to HTML }).catch(function (err) { console.log(err); }); function appendDataToHTML(data) { const filterContainer = document.getElementById("location__options"); const mainContainer = document.getElementById("jobListing"); // for each object, create card var locations_array = []; for (var i = 0; i < Object.keys(data.jobs).length; i++) { const job_title = data.jobs[i].title; const job_location = data.jobs[i].location.name; locations_array.push(job_location); console.log(locations_array[i]); const html = '<div class="card">'+ '<div class="card__body--title">'+ '<span class="jobTitle">' + job_title + '</span><br>' + '<span class="jobLocation">' + job_location + '</span>' + '</div>' + '</div>'; // const filter_html = '<option>' + locations_array [i]+ '</option>'; // document.getElementById("location__options").innerHTML = "<option>" + locations_array [i]+ "</option>"; mainContainer.insertAdjacentHTML('beforeend', html); } }
 select, .card{ margin-bottom: 30px; }
 <div class="row"> <div class="workable__filters"> <div class="workable__select"> <select> <option value="">OPEN POSITIONS</option> <div id="location__options"></div> </select> </div> </div> </div> <div class="row"> <div id="jobListing"></div> </div>

What I've tried:我试过的:

1. 1.

document.getElementById("location__options").innerHTML = "<option>" + locations_array [i]+ "</option>";

With the above I get the console error TypeError: Cannot set property 'innerHTML' of null有了上面我得到控制台错误TypeError: Cannot set property 'innerHTML' of null

2. 2.

const filterContainer = document.getElementById("location__options");
const filter_html = '<option>' + locations_array [i]+ '</option>';
filterContainer.insertAdjacentHTML('beforeend', filter_html);

With the above, I get the console error TypeError: Cannot read property 'insertAdjacentHTML' of null有了上面,我得到控制台错误TypeError: Cannot read property 'insertAdjacentHTML' of null

I just want the locations to be shown as options in the select我只想将位置显示为select中的options

You are pretty close, you just need something like this:你非常接近,你只需要这样的东西:

Change your select tag to look like this:将您的 select 标签更改为如下所示:

<select id="location__options">
    <option value="">OPEN POSITIONS</option>
</select>

this means we can target the select tag itself, we don't need a div.这意味着我们可以定位 select 标签本身,我们不需要 div。

Then you can create an optionTag every iteration of the loop and append it to the select tag:然后,您可以在循环的每次迭代中创建一个 optionTag,并将 append 设置为 select 标签:

const optionTag = document.createElement('option');
optionTag.value = job_title;
optionTag.innerHTML = job_title + " | " + job_location;

filterContainer.appendChild(optionTag);

Full example:完整示例:

 fetch('https://boards-api.greenhouse.io/v1/boards/cutover/jobs?content=true', {}).then(function (response) { return response.json(); // response type (json) }).then(function (data) { appendDataToHTML(data); // function that appends data to HTML }).catch(function (err) { console.log(err); }); function appendDataToHTML(data) { const filterContainer = document.getElementById("location__options"); const mainContainer = document.getElementById("jobListing"); // for each object, create card var locations_array = []; for (var i = 0; i < Object.keys(data.jobs).length; i++) { const job_title = data.jobs[i].title; const job_location = data.jobs[i].location.name; locations_array.push(job_location); console.log(locations_array[i]); const html = '<div class="card">'+ '<div class="card__body--title">'+ '<span class="jobTitle">' + job_title + '</span><br>' + '<span class="jobLocation">' + job_location + '</span>' + '</div>' + '</div>'; // const filter_html = '<option>' + locations_array [i]+ '</option>'; // document.getElementById("location__options").innerHTML = "<option>" + locations_array [i]+ "</option>"; const optionTag = document.createElement('option'); optionTag.value = job_title; optionTag.innerHTML = job_title + " | " + job_location; filterContainer.appendChild(optionTag); mainContainer.insertAdjacentHTML('beforeend', html); } }
 select, .card{ margin-bottom: 30px; }
 <div class="row"> <div class="workable__filters"> <div class="workable__select"> <select id="location__options"> <option value="">OPEN POSITIONS</option> </select> </div> </div> </div> <div class="row"> <div id="jobListing"></div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM