2016-01-25 86 views
0

我正在开发一个使用ASP.NET MVC的网页。当我使用aJax请求子页面内容时,url附加了一个“#”automatic.Before阿贾克斯,URL可能是这样的:http://a.travel.com/product/index,AJAX请求后,URL将是这样的:http://a.travel.com/product/index#。这是我的Ajax请求的代码:如何避免url aotomatic在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> 

为什么会出现这种情况,如何避免?

+2

[防止HREF = “#” 从改变URL散列链接](的可能的复制http://stackoverflow.com/questions/20215248/prevent-href-link-from-changing -the-url-hash) – JJJ

+0

你如何调用'GetProductByTag'? –

回答

1

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

如果你完全删除href元素它应该工作,它不会触发URL更改。

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

a:hover { 
cursor:pointer; 
} 

See here for some more info