Adding hyperlink in Javascript array of objects

家住魔仙堡 提交于 2020-06-01 06:53:32

问题


I have an array of objects and within each object there is an answers section, but I need to hyperlink some certain words that should send the user to a website.

I have given an example below.

faq: [
  {
    answers: "The capital is London"   
  }
]

So in the example above if you could show me how I can hyperlink the word London within Javascript it would be very helpful.

Many thanks!


回答1:


Just surround the word with anchor tag . Like this and give url after //

<a href="//www.google.com">London</a>



回答2:


Try this one. You can put your link instead of https://www.google.com. This will create hyperlink for word 'London'

 <script>
        function myFunction() {
            var result = "The capital is" + "London".link("https://www.google.com");
            document.getElementById("demo").innerHTML = result;
        }
        </script>


来源:https://stackoverflow.com/questions/53812940/adding-hyperlink-in-javascript-array-of-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!