如何正确转义 HTML 属性中的引号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4015345/
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
How do I properly escape quotes inside HTML attributes?
提问by Chris
I have a drop down on a web page which is breaking when the value string contains a quote.
当值字符串包含引号时,我在网页上有一个下拉菜单。
The value is "asd
, but in the DOM it always appears as an empty string.
值为"asd
,但在 DOM 中它始终显示为空字符串。
I have tried every way I know to escape the string properly, but to no avail.
我已经尝试了所有我知道的方法来正确地转义字符串,但无济于事。
<option value=""asd">test</option>
<option value="\"asd">test</option>
<option value=""asd">test</option>
<option value=""asd">test</option>
How do I render this on the page so the postback message contains the correct value?
我如何在页面上呈现它以便回发消息包含正确的值?
回答by Andy E
"
is the correct way, the third of your tests:
"
是正确的方法,第三个测试:
<option value=""asd">test</option>
You can see this working below, or on jsFiddle.
你可以在下面或jsFiddle上看到这个工作。
alert($("option")[0].value);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option value=""asd">Test</option>
</select>
Alternatively, you can delimit the attribute value with single quotes:
或者,您可以使用单引号分隔属性值:
<option value='"asd'>test</option>
回答by Lukasz Czerwinski
If you are using PHP, try calling htmlentities
or htmlspecialchars
function.
如果您使用的是 PHP,请尝试调用htmlentities
或htmlspecialchars
函数。
回答by aij
Per HTML syntax, and even HTML5, the following are all valid options:
<option value=""asd">test</option>
<option value=""asd">test</option>
<option value='"asd'>test</option>
<option value='"asd'>test</option>
<option value='"asd'>test</option>
<option value="asd>test</option>
<option value="asd>test</option>
Note that if you are using XML syntaxthe quotes (single or double) are required.
请注意,如果您使用XML 语法,则需要引号(单引号或双引号)。
回答by csonuryilmaz
Another option is replacing double quotes with single quotes if you don't mind whatever it is. But I don't mention this one:
如果您不介意它是什么,另一种选择是用单引号替换双引号。但我没有提到这个:
<option value='"asd'>test</option>
I mention this one:
我提到这个:
<option value="'asd">test</option>
In my case I used this solution.
在我的情况下,我使用了这个解决方案。
回答by Andrew
If you are using Javascript and Lodash, then you can use _.escape(), which escapes ", ', <, >, and &.
如果您使用 Javascript 和 Lodash,那么您可以使用 _.escape(),它转义 "、'、<、> 和 &。
See here: https://lodash.com/docs/#escape
请参阅此处:https: //lodash.com/docs/#escape
回答by Jim Manico
You really should only allow untrusted data into a whitelist of good attributes like: align, alink, alt, bgcolor, border, cellpadding, cellspacing, class, color, cols, colspan, coords, dir, face, height, hspace, ismap, lang, marginheight, marginwidth, multiple, nohref, noresize, noshade, nowrap, ref, rel, rev, rows, rowspan, scrolling, shape, span, summary, tabindex, title, usemap, valign, value, vlink, vspace, width
你真的应该只允许不受信任的数据进入良好属性的白名单,例如:align、alink、alt、bgcolor、border、cellpadding、cellspacing、class、color、cols、colspan、coords、dir、face、height、hspace、ismap、lang , marginheight, marginwidth, multi, nohref, noresize, noshade, nowrap, ref, rel, rev, rows, rowspan, scrolling, shape, span, summary, tabindex, title, usemap, valign, value, vlink, vspace, width
You really want to keep untrusted data out of javascript handlers as well as id or name attributes (they can clobber other elements in the DOM).
您真的希望将不受信任的数据以及 id 或 name 属性排除在 javascript 处理程序之外(它们可能会破坏 DOM 中的其他元素)。
Also, if you are putting untrusted data into a SRC or HREF attribute, then its really a untrusted URL so you should validate the URL, make sure its NOT a javascript: URL, and then HTML entity encode.
此外,如果您将不受信任的数据放入 SRC 或 HREF 属性,那么它确实是一个不受信任的 URL,因此您应该验证 URL,确保它不是 javascript: URL,然后是 HTML 实体编码。
More details on all of there here: https://www.owasp.org/index.php/Abridged_XSS_Prevention_Cheat_Sheet
此处有更多详细信息:https: //www.owasp.org/index.php/Abridged_XSS_Prevention_Cheat_Sheet
回答by Miguel
There is no way to escape quotes in the value of a input text... but you can use javascript (or jquery):
无法在输入文本的值中转义引号……但您可以使用 javascript(或 jquery):
<input type="input" name="myinput" id="myinput" value="" />
<script>document.getElementById("myinput").value="This input has a [\"]";</script>