1

JSFiddle

这是我正在尝试做的事情:

this.observableString = ko.observable('When $a \ne 0$, there are two solutions to \(ax^2 + bx + c = 0\) and they are $$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$');
    this.regularString = 'When $a \ne 0$, there are two solutions to \(ax^2 + bx + c = 0\) and they are $$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$';

};

两者都让我胡言乱语,因为反斜杠在被 MathJax 渲染之前以某种方式被替换了。如果我使用内联文本,它可以工作。如何在不触及反斜杠的情况下保存这些字符串?

4

2 回答 2

4

因为\是 JavaScript 中的转义字符,所以您必须对其进行转义才能获得反斜杠。

使用\\(转义转义字符)代替\.

看到这个更新的小提琴:http: //jsfiddle.net/LkqTU/9708/

于 2013-06-11T02:25:42.293 回答
4

您需要将所有反斜杠加倍。Javascript 使用 \ 作为“转义字符”,例如'\n'被解释为包含换行符的单字符字符串。

于 2013-06-11T02:25:55.547 回答