如何使用 Jquery 将 url 变成 div

how to get an url into a div using Jquery

我有一个动态 div,它从 Jquery 函数获取文本,有些像这样:

if(value.length == '') {
    error_text = 'This is not correct, please <a href="http://domain.com">contact us.</a>';
    $("#div_error").text(error_text);
} 

在 div 我得到 "This is not correct, please <"a href="http://domain.com">联系我们。".

我在 < 之后放置了一个 ",所以 Whosebug 不会像我希望的那样将其设为超链接....;-)

看起来不错,但我真正想要的是"This is not correct, please contact us."联系我们应该是一个超链接。

知道如何获得指向此 div 的有效超链接吗?

亲切的问候,

阿里

这是正确的方法:

if (value.length == '') {
    error_html = 'This is not correct, please <a href="http://domain.com">contact us.</a>';
    $("#div_error").html(error_html);
}

使用.text(),您强制将所有内容呈现为文本。请改用 .html(),这样您的 HTML 标签将被解析。