使用knockout.js,如何在绑定到段落<p>元素的文本属性的文本中包含回车符。
在我的ViewModel中,我生成了一个绑定到视图中的<p>的文本字符串。我想在浏览器显示的带有换行符的字符串中包含回车符。
在字符串中包含<br />或Environment.NewLine似乎不起作用。
发布于 2013-07-02 19:07:03
您可以使用html binding。
JS:
function AppViewModel() {
this.firstName = "Bert<br\>Test";
this.lastName = "Bertington";
}
// Activates knockout.js
ko.applyBindings(new AppViewModel());查看:
<p>First name: <strong data-bind="html: firstName">todo</strong></p>
<p>Last name: <strong>todo</strong></p>发布于 2013-07-02 18:29:24
您需要在元素中设置css属性。white-space: pre-wrap
<p style="white-space: pre-wrap">First name: <strong data-bind="text: firstName">todo</strong></p>
<p>Last name: <strong>todo</strong></p>
function AppViewModel() {
this.firstName = "Bert" + " \n " + "Test";
this.lastName = "Bertington";
}
// Activates knockout.js
ko.applyBindings(new AppViewModel());然后代码就可以工作了。使用\n
https://stackoverflow.com/questions/17422895
复制相似问题