如何正确转义 HTML 属性中的引号? [英] How do I properly escape quotes inside HTML attributes?
本文介绍了如何正确转义 HTML 属性中的引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当值字符串包含引号时,我在网页上有一个下拉菜单.
I have a drop down on a web page which is breaking when the value string contains a quote.
值为"asd
,但在DOM中它总是显示为空字符串.
The value is "asd
, but in the DOM it always appears as an empty string.
我已经尝试了所有我知道的方法来正确地转义字符串,但无济于事.
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?
推荐答案
"
是正确的方法,你的第三个测试:
"
is the correct way, the third of your tests:
<option value=""asd">test</option>
您可以在下面或 jsFiddle 上看到此工作.
You can see this working below, or on 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>
这篇关于如何正确转义 HTML 属性中的引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文