在HTML事件处理程序中转义双引号 [英] Escaping Double Quotes in HTML Event Handlers

查看:78
本文介绍了在HTML事件处理程序中转义双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,如何正确地转义

 < button onclick =foo(bar'c $ c>,它是一个字符串字面值, ))>>点击我< /按钮> 

因为我使用XHTML,所以不能使用单引号作为属性值。我可以使用单引号为字符串文字,但我想要一致。

c>< button onclick =foo(& quot; bar& quot;);>点击我< /按钮>

而且,您可以在XHTML中将它们混合使用,请在 W3 validator

 <?xml version =1.0encoding =utf-8?> 
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Strict // EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict。 DTD>
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< title> foo< / title>
< / head>
< body>
< div onclick ='bar(foo);'>< / div>
< / body>
< / html>

有些教程指出单引号无效,但不正确。


How do I escape double quotes in an event handler in HTML?

For example, how do I properly escape the bar, which is a string literal, in the following code?

<button onclick="foo("bar")")>Click Me</button>

I can't use single quotes for the attribute value since I'm using XHTML. I could use single quotes for string literals, but I'd like to be consistent.

解决方案

<button onclick="foo(&quot;bar&quot;);">Click Me</button>

And, you can mix them indeed in XHTML, try this in the W3 validator:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>foo</title>
</head>
<body>
<div onclick='bar("foo");'></div>
</body>
</html>

There are some tutorials which said single quotes are not valid, but they are incorrect.

这篇关于在HTML事件处理程序中转义双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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