简体   繁体   English

HTML编码导致Chrome中出现新行

[英]Html Encoding causing new lines in Chrome

In my signup form, I'm using asp.net unobtrusive validation to check if a username exists. 在我的注册表单中,我正在使用asp.net非侵入式验证来检查用户名是否存在。 My JsonResult method returns the following if a clash is found: 如果发现冲突,我的JsonResult方法将返回以下内容:

This is already in use. How about \u003cstrong\u003efoo123\u003c/strong\u003e?

I use Microsoft's unobtrusive validation to display this in my view: 我使用Microsoft的非侵入式验证在我的视图中显示此内容:

@Html.ValidationMessageFor(m => m.Username)

which causes it to appear as follows in the page source: 这会使它在页面源中显示如下:

This is already in use. How about
<strong>foo123</strong>
?

As you can see, the text is split over three lines. 如您所见,文本分为三行。 This is not a problem in Firefox, but in Chrome these new lines are causing the displayed text to break in a similar manner to <br /> . 在Firefox中这不是问题,但是在Chrome中,这些新行导致显示的文本以与<br />类似的方式中断。 I believe the encoding is to blame for this - can anyone explain why? 我相信应该为此负责编码-有人可以解释为什么吗? Is there any solution to this issue? 这个问题有解决方案吗?

Thank you in advance. 先感谢您。

If they are displayed with line breaks on the actual page, then first thing I would do is inspect <strong/> tag and see if there is any CSS that defines it as display:block; 如果它们在实际页面上显示为带有换行符,那么我要做的第一件事就是检查<strong/>标记,看看是否有任何CSS将其定义为display:block; or overrides white-space property. 或覆盖white-space属性。

If you are concerned about output of the DOM explorer... That's just how Chrome displays DOM trees. 如果您担心DOM资源管理器的输出...这就是Chrome显示DOM树的方式。 These three lines are each individual nodes (2 text nodes and one element node), so they are treated as equal. 这三行分别是单独的节点(2个文本节点和1个元素节点),因此将它们视为相等。 If you inspect the first paragraph of my answer, you will see the same thing. 如果您查看我的答案的第一段,您将看到相同的内容。

Without having tried it, you could try to replace newlines in the string with... nothing? 如果没有尝试过,您可以尝试用...什么代替字符串中的换行符?

@Html.ValidationMessageFor(m => m.Username).ToString().Replace(Environment.NewLine, "");

Feels really weird that would be the problem though since it's just in Chrome, double-check CSS rules. 确实很奇怪,但这可能是问题所在,因为它只是在Chrome中,请仔细检查CSS规则。 Or just surround the validation message in a p-tag? 还是仅将验证消息括在p标签中?

try tweaking with css. 尝试使用CSS进行调整。

 \u003cdiv\u003e 
  This is already in use. How about \u003cstrong\u003efoo123\u003c/strong\u003e?
\u003c/div\u003e?

I think this is due the some padding issue. 我认为这是由于某些填充问题引起的。 Upload the generated Html code of the page. 上载页面的生成的HTML代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM