I'm banging my head against the wall on this one and would like to know why when I enclose an HTML attribute, such as an id tag in single quotes: <div id='test'></div>
, they are automatically being replaced with double quotes: <div id="test"></div>
, when rendered and sent to the client for viewing.
我拿我的头往墙上撞,想知道为什么当我附上一个HTML属性,如一个id在单引号标记:< div id = '测试' > < / div >,他们会自动被替换为双引号:< div id = "测试" > < / div >,当呈现和发送到客户端查看。
Is there a way to enforce single quotes in attributes?
是否有办法在属性中强制使用单引号?
So far I have tried @Html.Raw("'test'")
, @Html.Raw("id='test'")
, and etc, and no matter what I do the quotes are still replaced. I see plenty of examples of single quotes being used successfully without automatic conversion, so I'm wondering what's going on and if anyone can provide resolution to this problem
到目前为止,我已经尝试了@Html.Raw(“test”),@Html.Raw(“id='test'”),等等,无论我做什么,引号仍然被替换。我看到很多单引号在没有自动转换的情况下被成功使用的例子,所以我想知道发生了什么,是否有人能解决这个问题
I'm using ASP.Net MVC 5, Bootstrap 3.0, Jquery, and Less, nothing too exotic in terms of javascript libraries, and the standard Razor view.
我用ASP。Net MVC 5, Bootstrap 3.0, Jquery,以及更少的javascript库和标准的Razor视图。
Thanks for your answers.
谢谢你的答案。
1 个解决方案
#1
1
The razor engine automatically escapes any HTML output, using Html.Raw
tells the engine to just output as is. You would need to do the entire block of HTML using Html.Raw
, not just the attribute e.g.
razor引擎使用HTML自动转义任何HTML输出。Raw告诉引擎按原样输出。您需要使用HTML来完成整个HTML块。不只是属性,例如。
@Html.Raw("<div id='test'></div>")
Alternatively, you could output your attribute as a literal e.g.
或者,您也可以将属性输出为文字。
<div @("id='active'")></div>
Interestingly it looks as though the browser is pre-processing the HTML and replacing the single quotes with doubles. I checked the result coming down from the server and I got
有趣的是,浏览器似乎在对HTML进行预处理,并用双引号替换单引号。我检查了服务器上的结果,我得到了
<div id='active'></div>
However, when I then viewed the HTML in the browser (Chrome 31 / IE 11) the single quotes had been replaced with double. This isn't just a display thing either because when I saved the output from the browser it still had double quotes.
然而,当我在浏览器中查看HTML (Chrome 31 / IE 11)时,单引号已经被双引号所取代。这不仅仅是一个显示的东西,因为当我从浏览器中保存输出时,它仍然有双引号。
This is probably the norm for most modern-day browsers and, unfortunately, it doesn't look like you have much control over this.
这可能是大多数现代浏览器的标准,不幸的是,看起来您没有太多的控制权。
#1
1
The razor engine automatically escapes any HTML output, using Html.Raw
tells the engine to just output as is. You would need to do the entire block of HTML using Html.Raw
, not just the attribute e.g.
razor引擎使用HTML自动转义任何HTML输出。Raw告诉引擎按原样输出。您需要使用HTML来完成整个HTML块。不只是属性,例如。
@Html.Raw("<div id='test'></div>")
Alternatively, you could output your attribute as a literal e.g.
或者,您也可以将属性输出为文字。
<div @("id='active'")></div>
Interestingly it looks as though the browser is pre-processing the HTML and replacing the single quotes with doubles. I checked the result coming down from the server and I got
有趣的是,浏览器似乎在对HTML进行预处理,并用双引号替换单引号。我检查了服务器上的结果,我得到了
<div id='active'></div>
However, when I then viewed the HTML in the browser (Chrome 31 / IE 11) the single quotes had been replaced with double. This isn't just a display thing either because when I saved the output from the browser it still had double quotes.
然而,当我在浏览器中查看HTML (Chrome 31 / IE 11)时,单引号已经被双引号所取代。这不仅仅是一个显示的东西,因为当我从浏览器中保存输出时,它仍然有双引号。
This is probably the norm for most modern-day browsers and, unfortunately, it doesn't look like you have much control over this.
这可能是大多数现代浏览器的标准,不幸的是,看起来您没有太多的控制权。