将String转换为HTML-将字符串转换为& quot;一个href& quot;元素 [英] Converting String to HTML - string to " a href" element
问题描述
您好,我在获取一些HTML链接添加到我的HTML页面时遇到了一些麻烦.我已经尝试过搜索,但到目前为止没有任何帮助.
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 load a snippet:
<div style="display: inline-block; color: rgb(0, 255, 144)">Roster: </div>
<span id="teamRoster"></span>
<br />
在视图中显示为 Roster:
的
现在我的代码段已修改为添加名称:
Right now my snippet has been modified to add names:
var rosterListings = "";
for (var i = 0; i < teamRoster.length; i++) {
rosterListings = rosterListings + teamRoster[i] + ", ";
}
$("#teamRoster").text(rosterListings);
将哪个视图更新为 Roster:John,Frank,Susan等.
但是,我现在尝试在每个人周围添加< a href>
标签,并将它们全部转换为实际链接.我的尝试看起来像这样
However, I am trying to now add <a href>
tag's around each person and turn them all into actual links. My attempt looks like this
var rosterListings = "";
for (var i = 0; i < teamRoster.length; i++) {
rosterListings = rosterListings + " <a href='" + idList[i] + "'>" + teamRoster[i] + "</a>,";
}
$("#teamRoster").text(rosterListings);
显示为
Roster:< a href ='#'> John</a> ;,< a href ='#'> Frank</a> ;,等等.
我理解为什么会这样,因为我设置了实际的文本/字符串,但是有没有办法将此字符串转换为HTML元素?我尝试了一些从Googling找到的 $.parseHTML
代码段,但是我必须将它们全部实现错误..::
I understand why this occurring since I am setting actual text/strings, but is there a way to convert this string into HTML elements? I have tried a few $.parseHTML
code snippets that I found from Googling but I must be implementing them all wrong.. :(
任何帮助表示感谢,谢谢!
Any help appreciated, Thank you!
推荐答案
好,解决方案很明显
只需更换
$("#teamRoster").text(rosterListings);
使用:
$("#teamRoster").html(rosterListings);
因为如果将其用作文本,则将其视为文本,如果您编写html,则将其视为html
Because if you use it as a text then it will treat it as the text and if you write html then it will treat it as a html
这篇关于将String转换为HTML-将字符串转换为& quot;一个href& quot;元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!