<script>
function postBackXTreeRequest(url)
{
alert(url);
}
</script>
<a class="tab0" target="_self" href="javascript:postBackXTreeRequest('?tvwID=A+%26+B');">test</a>
以上代码会提醒我A & B
,而不是A+%26+B
让我感到困惑。为什么会自动解码网址?
答案 0 :(得分:1)
href
属性的值是一个URL,因此JavaScript以URL编码。在执行JavaScript之前,URL将被解码。
不要将JavaScript放在href属性中。请改用不引人注目的JavaScript。
<a class="tab0" target="_self" href="/server/side/fallback">test</a>
<script>
// You'll want a more specific selector that `a` here, but predicting what would suit your HTML is impossible
document.querySelector('a').addEventListener('click', function (event) {
postBackXTreeRequest('?tvwID=A+%26+B');
event.preventDefault();
});
</script>
答案 1 :(得分:0)
在脚本中使用encodeURIComponent:
<a class="tab0" target="_self" href="javascript:postBackXTreeRequest('?tvwID=A+'+encodeURIComponent('%26')+'+B');">test</a>