简体   繁体   中英

how to add html anchor tag in javascript array concept

var n = [
    { id: "unep", 
    title: "UNEP", 
    body: " The United Nations Environment Programme (UN Environment) is the leading global environmental authority that sets the global environmental agenda,",
     img: "img/unep.png" 
    }

I'm trying to insert the anchor tag in the content of body but it doesn't seem to work. How can I insert the anchor tag?

If you want a text only output, you may create a new HTML element with createElement , then add required properties, and finally output text version of you freshly created tag with .outerHTML method:

var n = {
  id: "unep",
  title: "UNEP",
  body: "The United Nations Environment Programme … ",
  img: "img/unep.png"
};

const linkTag = document.createElement('a');
linkTag.href = 'https://google.com';
linkTag.textContent = 'Link';

const linkTagAsText = linkTag.outerHTML;

n.body = n.body + linkTagAsText;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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