如何在 .text() 属性中用 <a> 标签包装文本? jQuery

How to wrap text with <a> tag in the .text() attribute? jQuery

我有 $userMsg.text('@' + msg.user + ': ' + msg.txt),我想让用户名可以点击,这样,例如,我可以将页面定向到用户的个人资料。但是,当我在 msg.user 周围放置 <a> 标签时,它似乎不起作用。我怎样才能做到这一点?

$.text() 正是针对该文本。相反,使用 .html():

$userMsg.html('<a href="' + user.profile + '">@' + msg.user + '</a>: ' + user.msg)