0

我正在使用带有 html5 的 w3c 验证器。我有一个要传入 href 的数组afilter[]=abc,我尝试如下转义括号:

<a href='slideshowform.php?x=y&amp;afilter&#91;&#93;=abc'>phases of matter</a>

但我仍然收到错误:

 Bad value slideshowform.php?x=y&afilter[]=abc for attribute href on element a: Illegal character in query component.

如何在不出错的情况下传递数组 - 或者我是否错误地转义了括号?

4

1 回答 1

1

You have to URL encode it, not HTML encode it. Your URL would have to look like the following:

slideshowform.php?x=y&afilter%5B%5D=abc

Most programming languages have stuff like this built in (e.g. rawurlencode() in PHP or encodeURI in JavaScript) or you can simply use an online service like (no affiliation, just one of the first search results) http://www.url-encode-decode.com/

Of course it’s a good idea to encode the HTML reserved characters for outputting the link in an HTML document as well. So you’d end up with the following URL within your HTML document.

slideshowform.php?x=y&amp;afilter%5B%5D=abc
于 2013-11-11T23:31:42.620 回答