Javascript JS-为什么不起作用-有bug吗?

Javascript JS-为什么不起作用-有bug吗?,javascript,django,Javascript,Django,我有这个代码,它将page=value搜索参数添加到url并加载页面。但这不起作用,它没有设置url,我有任何打字错误吗 $(function(){ $('.page').on('click',function(){ var page = $(this).text(); var url = String(window.location); var newurl = ""; if(url.indexOf("?") !

我有这个代码,它将
page=value
搜索参数添加到url并加载页面。但这不起作用,它没有设置url,我有任何打字错误吗

$(function(){
     $('.page').on('click',function(){
         var page = $(this).text();
         var url = String(window.location);
         var newurl = "";
         if(url.indexOf("?") !== -1){
              if(url.indexOf('page') !== -1){
                 newurl = url.replace(/([&?]page=)[^&]*/, "$1" + String(page));
                 window.location = newurl;                            
              }else{
                 newurl = url +'&page='+String(page);
                 window.location = newurl;
              }
         }else{
             newurl = url +'?page='+String(page);
             window.location = newurl;

          }
        });
  });
html


浏览器正在跟踪链接的href。 使用preventDefault修复脚本

$(function(){
     $('.page').on('click',function(e){
     e.preventDefault();
         var page = $(this).text();
         var url = String(window.location);
         var newurl = "";
         if(url.indexOf("?") !== -1){
              if(url.indexOf('page') !== -1){
                 newurl = url.replace(/([&?]page=)[^&]*/, "$1" + String(page));
                 window.location = newurl;                            
              }else{
                 newurl = url +'&page='+String(page);
                 window.location = newurl;
              }
         }else{
             newurl = url +'?page='+String(page);
             window.location = newurl;

          }
        });
  });

什么东西不太管用?@adeneo,正如我所说,它没有设置URL。在发布之前,您应该使用控制台检查输入错误。@DSG,我已经做了。我没有看到任何错误,然后我在这里发布了您需要防止默认操作发生
$('.page')。在('click',函数(e){e.preventDefault()
。如果没有,将发生默认操作(在本例中为页面重新加载)。因此问题
$(function(){
     $('.page').on('click',function(e){
     e.preventDefault();
         var page = $(this).text();
         var url = String(window.location);
         var newurl = "";
         if(url.indexOf("?") !== -1){
              if(url.indexOf('page') !== -1){
                 newurl = url.replace(/([&?]page=)[^&]*/, "$1" + String(page));
                 window.location = newurl;                            
              }else{
                 newurl = url +'&page='+String(page);
                 window.location = newurl;
              }
         }else{
             newurl = url +'?page='+String(page);
             window.location = newurl;

          }
        });
  });