JavaScript 警报函数中的 html 标记

Html tags in javascript alert function

本文关键字:html 标记 函数 JavaScript      更新时间:2023-09-26

这是我的aspx.cs代码:

string script = "alert('Dosya/Dosyalar Başarı İle Yüklendi'n " + btnUpload.FileName + " ');";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);

但它不起作用。(无错误)当我删除'运算符时,其工作警报功能。我想在文本下方查看文件名

我该怎么做?

尝试''r'换新行:

string script = "alert('Dosya/Dosyalar Başarı İle Yüklendi ''r''n " + btnUpload.FileName + " ');";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);

乔恩·斯基特(Jon Skeet)回到2004年的C#转义序列

C# defines the following character escape sequences:
'' - single quote, needed for character literals
'" - double quote, needed for string literals
'' - backslash
'0 - Unicode character 0
'a - Alert (character 7)
'b - Backspace (character 8)
'f - Form feed (character 12)
'n - New line (character 10)
'r - Carriage return (character 13)
't - Horizontal tab (character 9)
'v - Vertical quote (character 11)
'uxxxx - Unicode escape sequence for character with hex value xxxx
'xn[n][n][n] - Unicode escape sequence for character with hex value nnnn (variable length version of 'uxxxx)
'Uxxxxxxxx - Unicode escape sequence for character with hex value xxxxxxxx (for generating surrogates)

您可以使用System.Environment.NewLine而不是'。例:

string script = "alert('Dosya/Dosyalar Başarı İle Yüklendi "+ System.Environment.NewLine + btnUpload.FileName + " ');";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);