如何避免 url 自动在 ajax 请求后附加一个“#”符号

How to avoid url aotomatic append a "#" symbol after ajax request

本文关键字:一个 符号 url 何避免 请求 ajax      更新时间:2023-09-26

我正在使用 ASP.NET MVC开发一个网页.当我使用aJax请求子页面内容时,url会自动附加一个"#"。在 aJax 之前,url 会这样:http://a.travel.com/product/index,在 aJax 请求之后,url 会这样:http://a.travel.com/product/index#。这是我的 aJax 请求代码:

 function GetProductByTag(tagId, source) {
        var keyword = $("#serachInput").val();
        if (keyword == null) {
            return;
        }
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    document.getElementById("productList").innerHTML = xhr.responseText;
                } else {
                    xhr.abort();
                    alert("Too many request!");
                }
            }
        }   
        xhr.open("Post", "/Product/ProductList?tagId=" + tagId + "&pageIndex=1", true);                
        }
        xhr.send(null);
    }

响应文本是产品列表信息(html)。

这是我的页面列表代码的一部分:

               <li onclick="ClickProductList(1,'http://www.ly.com/scenery/BookSceneryTicket_2287.html?spm=1.68076534.156.4&amp;refid=75866398')" class="top">
                        <a href="#">
                            <img src="/WCF_UploadFile/TourProductPhoto/20160125143157LWTIW.jpg">
                            <h6>                                    
                                <span>35</span>
                            </h6>
                            <p></p>
                        </a>
                    </li>

为什么会发生这种情况以及如何避免?

如果触发事件的锚元素有 href="#",那么它会导致浏览器像这样操作。

如果您完全删除 href 元素,它应该可以工作,并且不会触发 url 更改。

但是,您可能需要更改 CSS,因为没有 href 的锚点会丢失一些样式,例如光标。

a:hover {
 cursor:pointer;
}

有关更多信息,请参见此处