是否可以在alert()对话框中显示上标字符?

发布于 2024-08-03 05:01:45 字数 457 浏览 4 评论 0 原文

是否可以在 JavaScript 的 alert()confirm()prompt() 对话框中显示上标字符(不仅仅是数字) ?

由于某些原因,我需要插入文本:

2 后跟上标“n” 2^n

进入 JavaScript 警报、确认和提示框。快速谷歌搜索确实有帮助,但不完全是我找到了一种使用 Unicode \u00B 字符在对话框中显示上标数字的方法,但它不适用于字符

alert('2\u00B2'); shows correctly 2^2
alert('2\u00Bn'); shows 2u00Bn

所以目标是显示上标字符而不是数字。


^ 用作 Power 并显示下一个字符是上标,以防万一有人感到困惑。

Is it possible to display superscripted characters (not only numbers) in the alert(), confirm() or prompt() dialogue boxes in JavaScript?

Due to some reasons I need to insert a text:

2 followed by superscripted 'n'
2^n

Into JavaScript alert, confirm and prompt boxes. Fast google searching did help but not exactly I found a way to display superscripted numbers in dialogue boxes using Unicode \u00B character but it doesn't work with characters

alert('2\u00B2'); shows correctly 2^2
alert('2\u00Bn'); shows 2u00Bn

So the goal is to show a character superscripted not the number.


^ is used as Power and to show that next character is superscripted, just in case someone gets confused.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

紅太極 2024-08-10 05:01:45

该字符代码没有什么神奇之处 - 它只是恰好是为“上标二”字符选择的代码。还有一个“上标三”(\u00B3) (³)、一个“上标一”(\00B9) (¹) 和一个“上标拉丁小写字母 N” (\u207F) (ⁿ)。但如果您需要其他上标,那么您就不走运了 - alert() 不会呈现 HTML,因此您只能使用 Unicode 定义的字符。

您最好完全放弃 alert() 并在页面本身内模拟模式对话框。许多库已经提供了此功能,包括优秀的 jQuery UI Dialog

There's nothing magical about that character code - it just happens to be the one picked for the "Superscript two" character. There's also a "Superscript three" (\u00B3) (³) a "Superscript one" (\00B9) (¹), and a "Superscript Latin Small Letter N" (\u207F) (ⁿ). But if you need some other superscript, you're out of luck - alert() doesn't render HTML, so you're limited to the characters defined by Unicode.

You might be better off abandoning alert() entirely, and simulating a modal dialog within the page itself. Many libraries exist to provide this functionality already, including the excellent jQuery UI Dialog.

碍人泪离人颜 2024-08-10 05:01:45

,没有可靠的方法将上标文本放入警报框中。您当然可以使用 HTML 标记在页面上执行此操作,但在警报框中您会受到更多限制。

问题是:没有“Unicode \u00B”字符。您使用的字符是 \u00B2 (定义为“上标二”)。事实上最后有一个“2”本质上是巧合。例如,代码点 \u00B1 是不相关的(它是加号/减号)。

还有一些其他字符在 Unicode 中具有特定的上标版本,您可以在 此搜索,但并非每个字母都有上标版本。

No, there's no reliable way of getting superscript text into an alert box. You could certainly do it on the page using the HTML <sup> tag, but in an alert box you're more limited.

The problem is: there is no "Unicode \u00B" character. The character you're using is \u00B2 (defined as "Superscript Two"). The fact that there's a two at the end is essentially coincidental. The code point \u00B1, for example, is unrelated (it's the plus/minus sign).

There are a few other characters that have specific superscript versions in Unicode, which you can find in this search, but there aren't superscript versions of every letter.

傻比既视感 2024-08-10 05:01:45

Unicode,或UTF-8需要两对字符,或四个十六进制数字来组成一个字符; 00B2 被识别为“上标 2”,而 00Bn 是无效的十六进制值。上标数字 1-3 为:00B900B200B3

不幸的是,没有神奇的上标前缀可以使后面的字符成为上标。只有 HTML 可以做到这一点,并且正如 Shog 所说,alert 呈现纯文本。

Unicode, or UTF-8 requires two pairs of characters, or four hexadecimal digits to make a character; 00B2 is recognized as "superscript 2" whereas 00Bn is an invalid hexadecimal value. The superscript numbers 1-3 are: 00B9, 00B2, and 00B3.

Unfortunately there is no magical superscript-prefix that will make the following character superscript. Only HTML <sup> can do that and, like Shog said, alert renders plain text.

单身情人 2024-08-10 05:01:45

我最终编写了这段代码来生成 unicode 上标整数:

const unicodeSuperscriptNumbers = [
    "\u2070",
    "\u00B9",
    "\u00B2",
    "\u00B3",
    "\u2074",
    "\u2075",
    "\u2076",
    "\u2077",
    "\u2078",
    "\u2079",
  ];

  function superscript(n: number) {
    if (!n) throw "no number provided";
    if (!Number.isInteger(n)) throw "We don't bother with non-integer superscripts for now";
    if (n < 0) return "\u207B" + superscript(n * -1);
    if (n > 10) return superscript(~~(n / 10)) + superscript(n % 10);
    return unicodeSuperscriptNumbers[n];
  }

如果您还需要其他字符, 这篇博文列出了大多数(但不是全部)大写和小写上标 unicode 字母,并且扩展上面的代码以处理其他字符相对简单。

I ended up writing this code for generating unicode superscript integers:

const unicodeSuperscriptNumbers = [
    "\u2070",
    "\u00B9",
    "\u00B2",
    "\u00B3",
    "\u2074",
    "\u2075",
    "\u2076",
    "\u2077",
    "\u2078",
    "\u2079",
  ];

  function superscript(n: number) {
    if (!n) throw "no number provided";
    if (!Number.isInteger(n)) throw "We don't bother with non-integer superscripts for now";
    if (n < 0) return "\u207B" + superscript(n * -1);
    if (n > 10) return superscript(~~(n / 10)) + superscript(n % 10);
    return unicodeSuperscriptNumbers[n];
  }

If you need other characters as well, this blog post lists most, but not all uppercase and lowercase superscript unicode letters, and it's relatively straightforward to extend the code above so it handles other characters.

萧瑟寒风 2024-08-10 05:01:45

npm 包 numbers-to-superscript 将指定字符串中的所有数字转换为上标。

这里可能是一个选择。

The npm package numbers-to-superscript converts all numbers in the specified string to superscript.

Might be an option here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文