[英]using Javascript fetch api how can I add a hyperlink to each element o f returned array?
Using fetch, I get the following array from an api call: 使用访存,我从api调用中获取以下数组:
[
"BN16 1AA",
"BN16 1AB",
"BN16 1AD",
"BN16 1AE",
"BN16 1AF",
"BN16 1AG",
"BN16 1AH",
"BN16 1AJ",
"BN16 1AL",
"BN16 1AQ"
]
here it appears vertically but when I add it to my web page with 在这里它垂直出现,但是当我将其添加到我的网页时
document.body.append(myarr[i])
it appears horizontally like this: 它像这样水平出现:
BN16 1AABN16 1ABBN16 1ADBN16 1AEBN16 1AFBN16 1AGBN16 1AHBN16 1AJBN16 1ALBN16 1AQ
So I would like to do two things: 所以我想做两件事:
1) present each item vertically and 2) add either a hyperlink: <a href=myfunc(val)>val</a>
or onclick event preferably 1)垂直显示每个项目,以及2)最好添加超链接:
<a href=myfunc(val)>val</a>
或onclick事件
Here is the full code: 这是完整的代码:
fetch('http://api.postcodes.io/postcodes/BN16/autocomplete') .then(response => response.json()) .then(data => { arr = data.result let myarr = arr; for (let i = 0; i < myarr.length; i++) { document.body.append(myarr[i]) } });
I have attempted, in multiple ways, to add the hyperlink including: 我试图以多种方式添加超链接,包括:
document.body.append(myarr[i]).onclick = myfunc()
Many ways to skin a cat. 剥皮猫的方法很多。
const myFunc = str => console.log(str); const myarr = ["BN16 1AA", "BN16 1AB", "BN16 1AD", "BN16 1AE", "BN16 1AF", "BN16 1AG", "BN16 1AH", "BN16 1AJ", "BN16 1AL", "BN16 1AQ"]; myarr.forEach(code => { let a = document.createElement("a"); a.textContent = code; a.href = "#"; a.setAttribute("data-code", code); a.onclick = function(e) { e.preventDefault(); myFunc(this.getAttribute("data-code")) } document.body.append(a); document.body.append(document.createElement("br")); }); document.body.append(document.createElement("hr")); // Or delegated let div = document.createElement("div"); div.id = "container" document.body.append(div); let html = myarr.map(code => `<a href="#" data-code="${code}">${code}</a><br/>`) div.innerHTML = html.join(""); div.addEventListener("click", (e) => { if (e.target.tagName === "A") { e.preventDefault(); myFunc(e.target.getAttribute("data-code")); } });
I do not like using hyperlinks only to trigger a function, so I would prefer adding a click event to the list. 我不喜欢仅使用超链接来触发功能,因此我希望将click事件添加到列表中。
const myFunc = value => { console.log( `the user clicked ${ value }` ); }; const arr = [ "BN16 1AA", "BN16 1AB", "BN16 1AD", "BN16 1AE", "BN16 1AF", "BN16 1AG", "BN16 1AH", "BN16 1AJ", "BN16 1AL", "BN16 1AQ" ]; const items = arr.map( value => `<li data-value="${ value }">${ value }</li>` ); const list = document.createElement( 'ul' ); list.innerHTML = items.join( '' ); list.addEventListener( 'click', event => { const value = event.target.getAttribute( 'data-value' ); if ( value ) myFunc( value ); }); document.body.appendChild( list );
You could add a table to get data row wise and put onclick
with the anchor HTML: 您可以添加一个表以获取数据行,并使用锚点HTML放置
onclick
:
fetch('http://api.postcodes.io/postcodes/BN16/autocomplete') .then(response => response.json()) .then(data => { arr = data.result let myarr = arr; var table = document.createElement("table"); for (let i = 0; i < myarr.length; i++) { var anchor = document.createElement("a"); anchor.setAttribute("onclick", "return myFunc('" + myarr[i] + "')"); anchor.innerHTML = myarr[i]; anchor.href = "#"; table.innerHTML += '<tr><td>' + anchor.outerHTML + '<td><td>'; } document.body.append(table); }); function myFunc(val) { console.log(val); }
You can try this :
您可以尝试以下方法:
fetch('http://api.postcodes.io/postcodes/BN16/autocomplete') .then(response => response.json()) .then(data => { data.result.forEach((item) => { document.body.innerHTML += '<br><a href="#">' + item + '</a>'; }) });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.