javascript 强制在一个 <td> 中为文本换行

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

Forcing a new line for text in one <td>

javascripthtmlasp.netcss

提问by Ilia Koulikov

I have been searching for about 20 minutes, with no luck on answering this question. I would very much appreciate anyone's input on this.

我已经搜索了大约 20 分钟,没有回答这个问题。我非常感谢任何人对此的意见。

I have one cell (td) inside of a table row, and that cell will contain one line of text most of the time, but will occasionally contain two lines (the exact text that it contains depends on server-side processing). I don't mind so much if the text of one line is mushed together and wraps around, but I need to be able to start a new line if a second line is added. Hence my attempts to use the newline character below.

我在表格行中有一个单元格 (td),该单元格大部分时间将包含一行文本,但偶尔会包含两行(它包含的确切文本取决于服务器端处理)。如果一行的文本混在一起并环绕,我并不介意,但是如果添加第二行,我需要能够开始一个新行。因此我尝试使用下面的换行符。

I can't really break it up into two <td>elements, and I can't seem to get \nor <br/>to work when I include them in the text server-side. If I do that, they are just rendered as plain text inside the cell.

我真的不能把它分成两个<td>元素,当我将它们包含在文本服务器端时,我似乎无法获得\n<br/>工作。如果我这样做,它们只会在单元格内呈现为纯文本。

Essentially, I want to be able to avoid analyzing the text with javascript client-side to add special characters. I am not even sure if that would work. Is there some css/html/javascript/jquery trick that might do the job?

本质上,我希望能够避免使用 javascript 客户端分析文本以添加特殊字符。我什至不确定这是否可行。是否有一些 css/html/javascript/jquery 技巧可以完成这项工作?

The C# code that builds the text is simple enough, here's one example:

构建文本的 C# 代码非常简单,下面是一个示例:

    caseDueDate = "Reading Response due: " + readingDueDate.ToString();

The Razor code for the field looks like this:

该字段的 Razor 代码如下所示:

    <td>@Model.patients.ElementAt(ii).CaseDue</td>

The correctness of the results coming back is just fine. T'is only the formatting... =)

返回的结果的正确性很好。T'只是格式... =)

采纳答案by Jason Fingar

Sounds like something is converting your <br />tags to htmlentities at some point in your code, as these should not render as plain text in html output.

听起来像是在<br />代码中的某个点将您的标签转换为 htmlentities,因为这些不应在 html 输出中呈现为纯文本。

Is your output wrapped in an htmlentities()call or something similar?

您的输出是否包含在htmlentities()电话或类似内容中?

回答by NetsydeMiro

BR tags withing TDs should work... the server-side code is probably automatically escaping the html to prevent cross-site scripting. There are different ways to selectively disable this depending on the specific server software being used.

带有 TD 的 BR 标签应该可以工作……服务器端代码可能会自动转义 html 以防止跨站点脚本。根据所使用的特定服务器软件,有不同的方法可以有选择地禁用它。

回答by Ben Griffiths

You're using ASP.NET MVC on the server, right?

您在服务器上使用 ASP.NET MVC,对吗?

Two options spring to mind, if you want to manually control the use of line breaks on the server side:

如果您想在服务器端手动控制换行符的使用,我会想到两个选项:

(1) You could write a display template (partial view) for CaseDue property. If you mark your model with the UIHint attribute, like so:

(1) 您可以为 CaseDue 属性编写一个显示模板(部分视图)。如果您使用 UIHint 属性标记您的模型,如下所示:

[UIHint("MyPartialView")]
public string CaseDue { ... }

... and then use an HtmlHelper in your view, e.g.:

...然后在您的视图中使用 HtmlHelper,例如:

@Html.DisplayFor(m => m.patients.ElementAt(index).CaseDue)

(2) You could write an extension method for the HtmlHelper class which creates and returns an instance of MvcHtmlString. Razor doesn't encode these, so you could include whatever tags you want in the return value. e.g., roughly:

(2) 您可以为 HtmlHelper 类编写一个扩展方法,该类创建并返回 MvcHtmlString 的实例。Razor 不会对这些进行编码,因此您可以在返回值中包含您想要的任何标签。例如,大致:

public static MvcHtmlString TdForCaseDue(this HtmlHelper html, string[] lines) {
    TagBuilder td = new TagBuilder("td");
    td.InnerHtml = lines[0];
    for(int i = 1; i < lines.Length; i++) {
        td.InnerHtml += (new TagBuilder("br").ToString());
        td.InnerHtml += lines[i];
    }
    return new MvcHtmlString(td.ToString());
}