英文:
how can I dynamically insert data to html page from response array json?
问题
我正在制作一个Google搜索引擎克隆,有一个结果数组。我正在运行一个for循环,将数据输入到HTML页面中,并对ID进行编号,同时递增它们。虽然这个解决方案有效,但不是最优解。我如何根据结果数量动态插入数据?我正在使用原生JavaScript。
<div class="inside">
<a id="myLink5" href="https://www.example.com">示例链接</a>
<h1 id="titleinput5"></h1>
<div>
<p id="mydes5"> </p>
</div>
</div>
var input = document.getElementById("searchbartext");
var inputValue = input.value;
const url2 = 'https://api.bing.microsoft.com/v7.0/search?q=' + inputValue + '&count=50';
// totalEstimatedMatches
console.log(url2);
fetch(url2, options)
.then(response => response.json())
.then(data => {
console.log(data);
element.style.display = 'block';
var number = 1;
for (var i = 0; i < 7; i++) {
const title = data.webPages.value[i].name;
const organicUrl = data.webPages.value[i].url;
const image = data.webPages.value[i].snippet;
const totalEstimatedMatches = data.webPages.totalEstimatedMatches;
document.getElementById("number_of_results").innerHTML = totalEstimatedMatches;
document.getElementById("titleinput" + number).innerHTML = title;
// titleinput.textContent = ;
document.getElementById("mydes" + number).innerHTML = image;
document.getElementById("myLink" + number).href = organicUrl;
document.getElementById("myLink" + number).innerHTML = organicUrl;
number++;
}
})
英文:
I am working on a Google search engine clone and have an array of results. I am running a for loop to enter the data into an HTML page, and I have numbered the IDs while incrementing them. While this solution works, it is not optimal. How can I dynamically insert data based on the number of results? I am using vanilla JavaScript.
<div class="inside">
<a id="myLink5" href="https://www.example.com">Example Link</a>
<h1 id="titleinput5"></h1>
<div>
<p id="mydes5"> </p>
</div>
</div>
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var input = document.getElementById("searchbartext");
var inputValue = input.value;
const url2 = 'https://api.bing.microsoft.com/v7.0/search?q=' + inputValue + '&count=50';
// totalEstimatedMatches
console.log(url2);
fetch(url2, options)
.then(response => response.json())
.then(data => {
console.log(data);
element.style.display = 'block';
var number = 1;
for (var i = 0; i < 7; i++) {
const title = data.webPages.value[i].name;
const organicUrl = data.webPages.value[i].url;
const image = data.webPages.value[i].snippet;
const totalEstimatedMatches = data.webPages.totalEstimatedMatches;
document.getElementById("number_of_results").innerHTML = totalEstimatedMatches;
document.getElementById("titleinput" + number).innerHTML = title;
// titleinput.textContent = ;
document.getElementById("mydes" + number).innerHTML = image;
document.getElementById("myLink" + number).href = organicUrl;
document.getElementById("myLink" + number).innerHTML = organicUrl;
number++;
}
})
<!-- end snippet -->
答案1
得分: 1
使用document.createElement
下面我使用了示例响应数据,您可以在fetch方法中使用其中的逻辑。
还要遵循HTML结构。
const container = document.getElementById('result');
const count = document.getElementById('count');
// API响应数据示例
const webPages ={
totalEstimatedMatches:789456123,
value:[
{
id:'https://example.com',
name:'Example Website',
url:'https://google.com',
snippet:'Lorem iseuj something'
},
// 其他数据...
]
}
// 在循环外部使用count
count.innerText = webPages.totalEstimatedMatches;
for (var i = 0; i < 7; i++) {
const title = webPages.value[i].name;
const organicUrl = webPages.value[i].url;
const des = webPages.value[i].snippet;
const totalEstimatedMatches = webPages.totalEstimatedMatches;
let div = document.createElement('div');
let a = document.createElement('a');
let h1 = document.createElement('h1');
let p = document.createElement('p');
a.href = organicUrl;
a.innerText = organicUrl;
h1.innerText = title;
p.innerText = des;
div.append(a, h1, p);
container.append(div);
}
<section>
<span>Count</span><h5 id='count'></h5>
<main id='result' class='result'>
</main>
</section>
请注意,上述代码是JavaScript和HTML的混合代码,用于创建和填充HTML元素。
英文:
Use document.createElement
Below i have used sample response data, you can use the logic inside your fetch method.
Also follow the html structure
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const container = document.getElementById('result');
const count = document.getElementById('count');
//api response data sample
const webPages ={
totalEstimatedMatches:789456123,
value:[
{
id:'https://example.com',
name:'Example Website',
url:'https://google.com',
snippet:'Lorem iseuj something'
},
{
id:'https://example.com',
name:'Example Website',
url:'https://google.com',
snippet:'Lorem iseuj something'
},
{
id:'https://example.com',
name:'Example Website',
url:'https://google.com',
snippet:'Lorem iseuj something'
},
{
id:'https://example.com',
name:'Example Website',
url:'https://google.com',
snippet:'Lorem iseuj something'
},
{
id:'https://example.com',
name:'Example Website',
url:'https://google.com',
snippet:'Lorem iseuj something'
},
{
id:'https://example.com',
name:'Example Website',
url:'https://google.com',
snippet:'Lorem iseuj something'
},
{
id:'https://example.com',
name:'Example Website',
url:'https://google.com',
snippet:'Lorem iseuj something'
},
{
id:'https://example.com',
name:'Example Website',
url:'https://google.com',
snippet:'Lorem iseuj something'
},
{
id:'https://example.com',
name:'Example Website',
url:'https://google.com',
snippet:'Lorem iseuj something'
}
]
}
//count use outside the loop
count.innerText = webPages.totalEstimatedMatches;
for (var i = 0; i < 7; i++) {
const title = webPages.value[i].name;
const organicUrl = webPages.value[i].url;
const des = webPages.value[i].snippet;
const totalEstimatedMatches = webPages.totalEstimatedMatches;
let div = document.createElement('div');
let a = document.createElement('a');
let h1 = document.createElement('h1');
let p = document.createElement('p');
a.href = organicUrl;
a.innerText = organicUrl;
h1.innerText = title;
p.innerText = des
div.append(a,h1,p);
container.append(div)
}
<!-- language: lang-html -->
<section>
<span>Count</span><h5 id='count'></h5>
<main id='result' class='result'>
</main>
</section>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论