简体   繁体   English

如何在asp.net中呈现html

[英]How to render html in asp.net

My HTML is: (I'm using knockout) 我的HTML是:(我正在使用淘汰赛)

<p class="req" data-bind="text:loginError"></p> // show error text here

Js code is: Js代码是:

if(serverError == "Incorrect Password"){
    var link = $('<a/>').text("Forget password").attr('href', '/Account/ForgetPassword');
    loginError = "Incorrect password." + link[0].outerHTML + " ?";
}

But output is: 但输出是:

Incorrect password.<a href="/Account/ForgetPassword">Forget password</a> ?

How can I get output as link? 如何将输出作为链接?

Use "html" ko binding to display HTML elements: 使用“html”ko绑定来显示HTML元素:

<p class="req" data-bind="text:loginError, html: link"></p>

Then, in your code define both bindings elements: 然后,在您的代码中定义两个绑定元素:

if(serverError == "Incorrect Password"){
    loginError = "Incorrect password.";
    link = '<a href="/Account/ForgetPassword">Forgot password?</a>';
}

You're inserting a string loginError. 你正在插入一个字符串loginError。 You have to insert it as Control. 您必须将其作为Control插入。 Your placeholder is the 你的占位符是

$('p.req').append(logingError):

Did you mean 你的意思是

$('.req').html(link) ? $('。req')。html(链接)?

You are returning link[0].outerHTML that contains the html in text format. 您正在返回包含文本格式的html的link [0] .outerHTML。 If you use your var "link" it contains the jQuery object, so you use it into your p element called '.req'. 如果使用var“link”,它包含jQuery对象,因此您将它用于名为“.req”的p元素中。 Here is the example: jsfiddle.net/u8nqmmrk 这是一个例子:jsfiddle.net/u8nqmmrk

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM