Javascript 正在尝试组合href属性和location.search

Javascript 正在尝试组合href属性和location.search,javascript,jquery,html,href,Javascript,Jquery,Html,Href,我想将搜索查询结果添加到访问者在我的网站上单击的所有后续页面URL中。然而,当我的页面上有一个名为“directions.html”的人点击“关于我们”链接时,它会显示为: 说明.html?来宾# 问题是链接指向directions.html而不是about.html?有人知道是什么引起的吗 Javascript $("#aboutHtml").attr("href", "about.html" + location.search); HTML 试试这个: <a id="aboutHT

我想将搜索查询结果添加到访问者在我的网站上单击的所有后续页面URL中。然而,当我的页面上有一个名为“directions.html”的人点击“关于我们”链接时,它会显示为:

说明.html?来宾#

问题是链接指向directions.html而不是about.html?有人知道是什么引起的吗

Javascript

$("#aboutHtml").attr("href", "about.html" + location.search);
HTML


试试这个:

<a id="aboutHTML" onclick="newLocation();">About Us</a>

<script>
    function newLocation(){
      window.location = 'about.html'+ location.search;
    }
</script>
关于我们
函数newLocation(){
window.location='about.html'+location.search;
}

确保jQuery代码在链接加载后运行

$(document).ready(function() {
  $("#aboutHTML").attr("href", "about.html" + location.search);
});

调用之间Javascript如何保持location.search?您是否尝试过使用alert查看它是否仍然是您认为的值?这似乎是您最终使用directions.html而不是about.html的一个可能原因,但我认为这里还有其他问题。
$(document).ready(function() {
  $("#aboutHTML").attr("href", "about.html" + location.search);
});