Javascript 使用 ”?” 在 JS 文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6284128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using "?" in a JS file
提问by ChrisBedoya
I have this form dropdown thats populated by a JS file. One of the options in my dropdown has the character ?, but instead of the showing the letter it shows this: ?.
我有一个由 JS 文件填充的表单下拉列表。我的下拉列表中的选项之一具有字符? ,但不是显示字母,而是显示:?。
How can I get the form to show the letter ?? (something like an ascii code but for JS?)
我怎样才能得到表格来显示这封信?(类似于 ascii 代码但用于 JS?)
thanks in advance...
提前致谢...
回答by phihag
First of all, fixing the encoding of the page (to UTF-8, preferably with <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
directly after <head>
) and making sure your editor is set to UTF-8 is generally a very good idea.
首先,修复页面的编码(为 UTF-8,最好<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
直接在 之后<head>
)并确保您的编辑器设置为 UTF-8 通常是一个非常好的主意。
Otherwise, replacing ? with \u00f1
(in JavaScript code, not HTML) is the way to go.
否则,更换 ? with \u00f1
(在 JavaScript 代码中,而不是 HTML)是要走的路。
If the JavaScript gets the ? from an HTML or XML document, you can also use ñ
or ñ
there.
如果 JavaScript 得到 ? 从 HTML 或 XML 文档中,您也可以使用ñ
或ñ
。
回答by Félix Saparelli
You should use UTF-8 in your page, as mentioned several times by other answers and comments.
正如其他答案和评论多次提到的那样,您应该在页面中使用 UTF-8。
For the actual character, you can either use the HTML entity ñ
(again, as answered by others) or ñ
or ñ
(in HTML -- or in a JS string going to be inserted in or parsed as HTML), or the escaped UTF-8 value \u00F1
(in a JS string).
对于实际的字符,你可以使用HTML实体ñ
(同样,被别人当作回答)或ñ
或ñ
(在HTML中-或在JS字符串要在插入或解析为HTML),或转义的UTF-8值\u00F1
(在 JS 字符串中)。
回答by Tomasz Kowalczyk
You probably have wrong charset on your page. Check if you have something similar to this in your <head>
tag:
您的页面上可能有错误的字符集。检查您的<head>
标签中是否有类似的内容:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
You need to have it UTF-8 encoded to see characters contained in it.
您需要对其进行 UTF-8 编码才能查看其中包含的字符。
@phihag mentioned correctly that this answer will be only correct if the script is inline. For external scripts, you must ensure, that file is also UTF-8 encoded or include entity such as ñ
.
@phihag 正确地提到,只有当脚本是内联的时,这个答案才会正确。对于外部脚本,您必须确保该文件也是 UTF-8 编码或包含实体,例如ñ
.
回答by noinstance
Try the html entity for the character
尝试角色的 html 实体
ñ
回答by Pointy
You could use the character code:
您可以使用字符代码:
var babyGirl = "ni\u00f1a";
回答by Localghost
This is actually an HTML Entities issue. You need to provide the HTML entity instead of the character.
这实际上是一个 HTML 实体问题。您需要提供 HTML 实体而不是字符。
See: http://www.w3schools.com/tags/ref_entities.asp
请参阅:http: //www.w3schools.com/tags/ref_entities.asp
Specifically: &nt ilde; (minus the space) -> ?
具体来说: &nt ilde; (减去空格)-> ?