如果我在 jQuery 中使用 .html() 是否需要转义字符串?

Do I need to escape string if I use .html() in jQuery?

我的 js 文件中有以下行。

$( '#currency' ).html( '$' );

我还需要转义“$”字符串吗?无论如何,输出美元符号最安全的方法是什么?

如 JQuery 文档中所述:

Unlike the .html() method, .text() can be used in both XML and HTML documents. The result of the .text() method is a string containing the combined text of all matched elements. (Due to variations in the HTML parsers in different browsers, the text returned may vary in newlines and other white space.)

The .text() method cannot be used on form inputs or scripts. To set or get the text value of input or textarea elements, use the .val() method. To get the value of a script element, use the .html() method.

因此,如果您只想输出一个美元符号,.text() 将是安全的,因为它的值将被解析为字符串...