如何正确地在HTML属性中转义引号? [英] How do I properly escape quotes inside HTML attributes?

查看:62
本文介绍了如何正确地在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="&quot;asd">test</option>
<option value="&#34;asd">test</option>

怎么办我将其呈现在页面上,以便回发消息包含正确的值?

How do I render this on the page so the postback message contains the correct value?

推荐答案

& quot; 是正确的方法,即测试的三分之一:

&quot; is the correct way, the third of your tests:

<option value="&quot;asd">test</option>

您可以在下面或< a href = http://jsfiddle.net/AndyE/e5PPu/ rel = noreferrer> jsFiddle 。

alert($("option")[0].value);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
  <option value="&quot;asd">Test</option>
</select>

或者,您可以用单引号分隔属性值:

Alternatively, you can delimit the attribute value with single quotes:

<option value='"asd'>test</option>

这篇关于如何正确地在HTML属性中转义引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆