Html 格式化 JavaScript 警告框中的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17470817/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:46:41  来源:igfitidea点击:

Format the text in JavaScript alert box

javascripthtmlformatalert

提问by praveen

How can I format the text in a JavaScript alert box? I need a word in the text to be underlined.

如何格式化 JavaScript 警报框中的文本?我需要在文本中加下划线。

回答by vladkras

You can use only:

您只能使用:

\b = Backspace 
\f = Form feed 
\n = New line 
\r = Carriage return 
\t = tab
function underline(s) {
    var arr = s.split('');
    s = arr.join('\u0332');
    if (s) s = s + '\u0332';
    return s;
}

var str = underline('hello world'); // "h?e?l?l?o? ?w?o?r?l?d?"

alert(str);
0 = octal character \x00 = hexadecimal character \u0000 = hexadecimal unicode character

So, you can insert different ASCIIcharacters, but not format them (like italicor bold).

因此,您可以插入不同的ASCII字符,但不能对其进行格式化(如斜体粗体)。

EDITI also have to mention, that in fact alertacts like toStringconversion, so including tags and/or styles is not possible.

编辑我还必须提到,实际上alert就像toString转换一样,因此不可能包含标签和/或样式。

回答by Paul S.

Underline a string using unicode character '\u0332', called a COMBINING LOW LINE

使用 unicode 字符为字符串加下划线'\u0332',称为 aCOMBINING LOW LINE

alert('S\u0332earch - s underlined');

i18n = {};

i18n.SEARCH = "S\u0332earch - s underlined";

document.getElementById('search').innerHTML = i18n.SEARCH;

This method is not guaranteed to produce a smooth underline.

这种方法不能保证产生平滑的下划线。

Example of confirm()on Firefox 59:

confirm()在 Firefox 59 上的示例:

firefox confirm u0332

firefox 确认 u0332

回答by Igor

You can not style the text inside an alert box. But you can use a modal box solution instead, here's a list to get you started:

您不能设置警告框内的文本样式。但是您可以改用模态框解决方案,这里有一个列表可以帮助您入门:

回答by GuyT

In my case I created internationalization through a JavaScript file. I needed a mnemonic on each button so I was looking for an easy solution. Actually, I've an easy approach to get the wanted result:

就我而言,我通过 JavaScript 文件创建了国际化。我需要每个按钮上的助记符,所以我正在寻找一个简单的解决方案。实际上,我有一个简单的方法来获得想要的结果:

\u0332is the Unicode Character COMBINING LOW LINE

\u0332是 Unicode 字符组合低线

With this character you can underline single letters.

使用此字符,您可以在单个字母下划线。

Demonstration:

示范:

<button id="search"></button>
alert('Really need to alert some '+toUnicodeVariant('underlined', 'bold sans', 'underline')+' text');

回答by davidkonrad

Needed this badly, but none of the answers were particular helpful. As two answers suggest the solution is to use unicode, but you have to do this properly! I also wanted a more reuseable solution where I not needed to reinvent the wheel each and every time. So came up with this small javascript functionfor convenience.

非常需要这个,但没有一个答案特别有帮助。由于两个答案表明解决方案是使用 unicode,但您必须正确执行此操作!我还想要一个更可重用的解决方案,我不需要每次都重新发明轮子。所以为了方便起见,想出了这个小的javascript函数

<script src="https://rawgithub.com/davidkonrad/toUnicodeVariant/master/toUnicodeVariant.js"></script>
##代码##

Should produce something like this in all major browsers (try run the code snippet above) :

应该在所有主要浏览器中产生类似的东西(尝试运行上面的代码片段):

enter image description here

在此处输入图片说明

回答by Sirus64

You can't. Use external lib as JQuery UI Dialogor your own implementation.

你不能。使用外部库作为JQuery UI 对话框或您自己的实现。

回答by Bill Criswell

This is not possible. You'd have to use a plugin. If you have jQuery installed there's a bunch to choose from. If not this one looks promising: http://simplemodal.plasm.it/#examples

这不可能。你必须使用插件。如果您安装了 jQuery,则有很多可供选择。如果不是这个看起来很有希望:http: //simplemodal.plasm.it/#examples