且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何正确地转义html属性内的引号?

更新时间:2022-10-19 13:26:40

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

 < option value =&amp ;asd> test< / option> 

你可以看到这个工作在一个jsFiddle示例此处,或者,您可以使用单引号对属性值进行分隔:

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


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 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>

Any idea how to render this on the page so the postback message contains the correct value?

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

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

You can see this working in a jsFiddle example here. Alternatively, you can delimit the attribute value with single quotes:

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