Converting String to HTML - string to “ a href” element

前端 未结 3 1881
星月不相逢
星月不相逢 2021-01-18 16:10

Hello I am having some trouble getting some HTML links to add to my HTML page. I have tried searching around but nothing has helped thus far.

My page will initially

3条回答
  •  深忆病人
    2021-01-18 16:22

    The problem is that you're using .text(), which will insert only text into the span, as seen here.

    You need to use .html() if you want what is inserted to actually render as HTML.

    So, try this:

    $("#teamRoster").html(rosterListings);
    

    Demo

    Also note that the way you've set up your for loop causes an extra comma to be placed at the end of the list; I've fixed that here by checking whether it's the last element:

    if (i !== teamRoster.length - 1) {
            rosterListings = rosterListings + " " + teamRoster[i] + ",";
    } else {
            rosterListings = rosterListings + " and " + teamRoster[i] + ".";
    }
    

提交回复
热议问题