简体   繁体   English

如何使用Javascript Fetch API将超链接添加到返回数组的每个元素?

[英]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.

相关问题 如何向 javascript 消息添加超链接? - How can I add a hyperlink to a javascript message? 如何使用javascript或angular将一个数组的每个元素添加到其他数组中的对应元素 - How can I add each element of one array to corresponding elements in other arrays with javascript or angular 如何处理此JSON数组,以删除数组的每个元素中的包装器对象? 使用JavaScript - How can I process this JSON array to remove a wrapper object in each element of the array? using JavaScript 如何在数组中添加超链接(Javascript) - How to add hyperlink in array (Javascript) 如何使用 JavaScript 获取尚不存在的元素的样式? - How can I fetch the style of a not yet existing element using JavaScript? 如何将fourier.getEnergy() 的每个元素添加到数组中? - How can I add each element of fourier.getEnergy() into an array? 如何使用javascript fetch api插入数据 - how can i insert data using javascript fetch api 如何将 integer 值分配给使用 querySelectorAll 选择器返回的数组中的每个 HTML 元素 - How can I assign a integer value to each HTML element inside the array returned with querySelectorAll selector 如何使用JavaScript将字段添加到数组的每一行? - How can I add a field to each row of an array with Javascript? 使用 map 后,如何限制和插入从 API 获取请求返回的结果的条件 - How can I limit and insert conditions on results returned from API fetch request after using map
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM