通过添加单个字符进行javascript字符串操作

通过添加单个字符进行javascript字符串操作,javascript,jquery,Javascript,Jquery,在我的例子中,当您单击一个按钮时,它将在我的字符串上添加一个get参数,但是如果您再次单击,它将在参数的值上添加一个连字符。这实际上将用于降序和升序 <head> <link rel="stylesheet" href="style.css"> <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLU

在我的例子中,当您单击一个按钮时,它将在我的字符串上添加一个get参数,但是如果您再次单击,它将在参数的值上添加一个连字符。这实际上将用于降序和升序

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <p id ='demo'></p>
    <button column-data='age'>Age</button>
    <button column-data='height'>Height</button>
    <button column-data='weight'>Weight</button>

  <script>
    document.getElementById("demo").innerHTML = 
    window.location.href;
    $(function() {
      $('button').click(function(){
        data = $(this).attr('column-data');
        order_regex_asc = new RegExp('[&?]order=' + data, 'g')
        order_regex_desc = new RegExp('[&?]order=-' + data, 'g')
        _loc = window.location.href

        //if(_loc.indexOf('?order=-'))
        zinData = '?order=' +  data;
        zinNegativeData = '?order=' + "-" +  data;

        if(_loc.indexOf(zinData) == -1){
          _loc = _loc.replace(zinNegativeData, '');
          data =  zinData;
        }
        else{
          _loc = _loc.replace(zinData, '');
          data =  zinNegativeData;
        }
        _loc = _loc +  data;
                window.location = _loc
          document.getElementById("demo").innerHTML = _loc;

      });
    });
  </script>
 </body>
</html>
我目前的代码是:

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <p id ='demo'></p>
    <button column-data='age'>Age</button>
    <button column-data='height'>Height</button>
    <button column-data='weight'>Weight</button>
  </body>
  <script>
    document.getElementById("demo").innerHTML = window.location.href;
    $(function() {
      $('button').click(function(){
        data = $(this).attr('column-data');
        order_regex_asc = new RegExp('[&?]order=' + data, 'g')
        order_regex_desc = new RegExp('[&?]order=-' + data, 'g')
        _loc = window.location.href
        if(data){
          if (_loc.search(/[?]order/) == -1){  // Conditional to start GET parameter
                _loc = _loc + '?order=' + data;
                window.location = _loc
          }else{
              if ( _loc.search(order_regex_asc) != -1){ // Conditional to make this decending
                  console.log('dean');
                  // _loc = _loc.replace(order_regex_asc, '')
                  // data = '-'+data
              }else if (_loc.search(order_regex_desc) != -1){ // Conditional to make this ascending
                  console.log('armada');
                  // _loc = _loc.replace(order_regex_desc, '')
              }else{
                  window.location = _loc + '&order=' + data;
              }

          }
          document.getElementById("demo").innerHTML = _loc;
        }
      });
    });
  </script>
</html>
  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <p id ='demo'></p>
    <button column-data='age'>Age</button>
    <button column-data='height'>Height</button>
    <button column-data='weight'>Weight</button>

  <script>
    document.getElementById("demo").innerHTML = 
    window.location.href;
    $(function() {
      $('button').click(function(){
        data = $(this).attr('column-data');
        order_regex_asc = new RegExp('[&?]order=' + data, 'g')
        order_regex_desc = new RegExp('[&?]order=-' + data, 'g')
        _loc = window.location.href

        //if(_loc.indexOf('?order=-'))
        zinData = '?order=' +  data;
        zinNegativeData = '?order=' + "-" +  data;

        if(_loc.indexOf(zinData) == -1){
          _loc = _loc.replace(zinNegativeData, '');
          data =  zinData;
        }
        else{
          _loc = _loc.replace(zinData, '');
          data =  zinNegativeData;
        }
        _loc = _loc +  data;
                window.location = _loc
          document.getElementById("demo").innerHTML = _loc;

      });
    });
  </script>
 </body>
</html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <p id ='demo'></p>
    <button column-data='age'>Age</button>
    <button column-data='height'>Height</button>
    <button column-data='weight'>Weight</button>

  <script>
    document.getElementById("demo").innerHTML = 
    window.location.href;
    $(function() {
      $('button').click(function(){
        data = $(this).attr('column-data');
        order_regex_asc = new RegExp('[&?]order=' + data, 'g')
        order_regex_desc = new RegExp('[&?]order=-' + data, 'g')
        _loc = window.location.href

        //if(_loc.indexOf('?order=-'))
        zinData = '?order=' +  data;
        zinNegativeData = '?order=' + "-" +  data;

        if(_loc.indexOf(zinData) == -1){
          _loc = _loc.replace(zinNegativeData, '');
          data =  zinData;
        }
        else{
          _loc = _loc.replace(zinData, '');
          data =  zinNegativeData;
        }
        _loc = _loc +  data;
                window.location = _loc
          document.getElementById("demo").innerHTML = _loc;

      });
    });
  </script>
 </body>
</html>
年龄 高度 重量 document.getElementById(“demo”).innerHTML=window.location.href; $(函数(){ $(“按钮”)。单击(函数(){ data=$(this.attr('column-data'); order_regex_asc=new RegExp('[&?]order='+数据,'g') order_regex_desc=新的RegExp('[&?]order=-'+数据,'g') _loc=window.location.href 如果(数据){ 如果(_loc.search(/[?]order/)==-1){//有条件开始获取参数 _loc=_loc+'?订单='+数据; window.location=\u loc }否则{ 如果(_loc.search(order_regex_asc)!=-1){//有条件进行此降级 console.log('dean'); //位置=\u位置替换(订单\u regex\u asc') //数据='-'+数据 }else如果(_loc.search(order_regex_desc)!=-1){//有条件使其升序 控制台日志(“armada”); //位置=\u位置替换(订单\u regex\u desc') }否则{ window.location=_loc+'&order='+数据; } } document.getElementById(“demo”).innerHTML=\u loc; } }); });
这是我的礼物:

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <p id ='demo'></p>
    <button column-data='age'>Age</button>
    <button column-data='height'>Height</button>
    <button column-data='weight'>Weight</button>

  <script>
    document.getElementById("demo").innerHTML = 
    window.location.href;
    $(function() {
      $('button').click(function(){
        data = $(this).attr('column-data');
        order_regex_asc = new RegExp('[&?]order=' + data, 'g')
        order_regex_desc = new RegExp('[&?]order=-' + data, 'g')
        _loc = window.location.href

        //if(_loc.indexOf('?order=-'))
        zinData = '?order=' +  data;
        zinNegativeData = '?order=' + "-" +  data;

        if(_loc.indexOf(zinData) == -1){
          _loc = _loc.replace(zinNegativeData, '');
          data =  zinData;
        }
        else{
          _loc = _loc.replace(zinData, '');
          data =  zinNegativeData;
        }
        _loc = _loc +  data;
                window.location = _loc
          document.getElementById("demo").innerHTML = _loc;

      });
    });
  </script>
 </body>
</html>
例如,在my plunker中,如果单击“年龄”按钮,它将添加?order=age。如果您第二次单击它,则adde应该是?order=-age。如果您第三次单击它,那么它应该返回到?order=age。如果您继续单击,则在添加和删除连字符时,它将来回切换

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <p id ='demo'></p>
    <button column-data='age'>Age</button>
    <button column-data='height'>Height</button>
    <button column-data='weight'>Weight</button>

  <script>
    document.getElementById("demo").innerHTML = 
    window.location.href;
    $(function() {
      $('button').click(function(){
        data = $(this).attr('column-data');
        order_regex_asc = new RegExp('[&?]order=' + data, 'g')
        order_regex_desc = new RegExp('[&?]order=-' + data, 'g')
        _loc = window.location.href

        //if(_loc.indexOf('?order=-'))
        zinData = '?order=' +  data;
        zinNegativeData = '?order=' + "-" +  data;

        if(_loc.indexOf(zinData) == -1){
          _loc = _loc.replace(zinNegativeData, '');
          data =  zinData;
        }
        else{
          _loc = _loc.replace(zinData, '');
          data =  zinNegativeData;
        }
        _loc = _loc +  data;
                window.location = _loc
          document.getElementById("demo").innerHTML = _loc;

      });
    });
  </script>
 </body>
</html>
我被困在这里好几个小时了,很久没有用javascript编码了
<!DOCTYPE html>
<html>

  <head>    
    <link rel="stylesheet" href="style.css">
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script src="script.js"></script>
  </head>

  <body>
    <p id ='demo'></p>
    <button type="button" column-data='age'>Age</button>
    <button type="button" column-data='height'>Height</button>
    <button type="button" column-data='weight'>Weight</button>
     <script>
    document.getElementById("demo").innerHTML = window.location.href;
    $(function() {
      $('button').click(function(){
        data = $(this).attr('column-data');
        order_regex_asc = new RegExp('[&?]order=' + data, 'g')
        order_regex_desc = new RegExp('[&?]order=-' + data, 'g')
        _loc = window.location.href
        if(data){
          if (_loc.search(/[?]order/) == -1){  // Conditional to start GET parameter
                _loc = _loc + '?order=' + data;
                window.location = _loc
          }else{
              if (_loc.search(order_regex_asc) != -1){ // Conditional to make this decending
                  console.log('dean');
                  _loc = _loc.replace(data, '-'+data)
                  window.location=_loc;

              }else if (_loc.search(order_regex_desc) != -1){ // Conditional to make this ascending
                  console.log(armada);
                  _loc = _loc.replace('-'+data,data)
                  window.location=_loc;
                  // _loc = _loc.replace(order_regex_desc, '')
              }else{
                  window.location = _loc + '&order=' + data;
              }

          }
          document.getElementById("demo").innerHTML = _loc;
        }
      });
    });
  </script>
  </body>

</html>
  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <p id ='demo'></p>
    <button column-data='age'>Age</button>
    <button column-data='height'>Height</button>
    <button column-data='weight'>Weight</button>

  <script>
    document.getElementById("demo").innerHTML = 
    window.location.href;
    $(function() {
      $('button').click(function(){
        data = $(this).attr('column-data');
        order_regex_asc = new RegExp('[&?]order=' + data, 'g')
        order_regex_desc = new RegExp('[&?]order=-' + data, 'g')
        _loc = window.location.href

        //if(_loc.indexOf('?order=-'))
        zinData = '?order=' +  data;
        zinNegativeData = '?order=' + "-" +  data;

        if(_loc.indexOf(zinData) == -1){
          _loc = _loc.replace(zinNegativeData, '');
          data =  zinData;
        }
        else{
          _loc = _loc.replace(zinData, '');
          data =  zinNegativeData;
        }
        _loc = _loc +  data;
                window.location = _loc
          document.getElementById("demo").innerHTML = _loc;

      });
    });
  </script>
 </body>
</html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <p id ='demo'></p>
    <button column-data='age'>Age</button>
    <button column-data='height'>Height</button>
    <button column-data='weight'>Weight</button>

  <script>
    document.getElementById("demo").innerHTML = 
    window.location.href;
    $(function() {
      $('button').click(function(){
        data = $(this).attr('column-data');
        order_regex_asc = new RegExp('[&?]order=' + data, 'g')
        order_regex_desc = new RegExp('[&?]order=-' + data, 'g')
        _loc = window.location.href

        //if(_loc.indexOf('?order=-'))
        zinData = '?order=' +  data;
        zinNegativeData = '?order=' + "-" +  data;

        if(_loc.indexOf(zinData) == -1){
          _loc = _loc.replace(zinNegativeData, '');
          data =  zinData;
        }
        else{
          _loc = _loc.replace(zinData, '');
          data =  zinNegativeData;
        }
        _loc = _loc +  data;
                window.location = _loc
          document.getElementById("demo").innerHTML = _loc;

      });
    });
  </script>
 </body>
</html>
年龄 高度 重量 document.getElementById(“demo”).innerHTML=window.location.href; $(函数(){ $(“按钮”)。单击(函数(){ data=$(this.attr('column-data'); order_regex_asc=new RegExp('[&?]order='+数据,'g') order_regex_desc=新的RegExp('[&?]order=-'+数据,'g') _loc=window.location.href 如果(数据){ 如果(_loc.search(/[?]order/)==-1){//有条件开始获取参数 _loc=_loc+'?订单='+数据; window.location=\u loc }否则{ 如果(_loc.search(order_regex_asc)!=-1){//有条件进行此降级 console.log('dean'); _loc=_loc.replace(数据“-”+数据) 窗口位置=_loc; }else如果(_loc.search(order_regex_desc)!=-1){//有条件使其升序 控制台日志(无敌舰队); _loc=_loc.replace('-'+数据,数据) 窗口位置=_loc; //位置=\u位置替换(订单\u regex\u desc') }否则{ window.location=_loc+'&order='+数据; } } document.getElementById(“demo”).innerHTML=\u loc; } }); });

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <p id ='demo'></p>
    <button column-data='age'>Age</button>
    <button column-data='height'>Height</button>
    <button column-data='weight'>Weight</button>

  <script>
    document.getElementById("demo").innerHTML = 
    window.location.href;
    $(function() {
      $('button').click(function(){
        data = $(this).attr('column-data');
        order_regex_asc = new RegExp('[&?]order=' + data, 'g')
        order_regex_desc = new RegExp('[&?]order=-' + data, 'g')
        _loc = window.location.href

        //if(_loc.indexOf('?order=-'))
        zinData = '?order=' +  data;
        zinNegativeData = '?order=' + "-" +  data;

        if(_loc.indexOf(zinData) == -1){
          _loc = _loc.replace(zinNegativeData, '');
          data =  zinData;
        }
        else{
          _loc = _loc.replace(zinData, '');
          data =  zinNegativeData;
        }
        _loc = _loc +  data;
                window.location = _loc
          document.getElementById("demo").innerHTML = _loc;

      });
    });
  </script>
 </body>
</html>
年龄 高度 重量 document.getElementById(“demo”).innerHTML=window.location.href; $(函数(){ $(“按钮”)。单击(函数(){ data=$(this.attr('column-data'); order_regex_asc=new RegExp('[&?]order='+数据,'g') order_regex_desc=新的RegExp('[&?]order=-'+数据,'g') _loc=window.location.href 如果(数据){ 如果(_loc.search(/[?]order/)==-1){//有条件开始获取参数 _loc=_loc+'?订单='+数据; window.location=\u loc }否则{ 如果(_loc.search(order_regex_asc)!=-1){//有条件进行此降级 console.log('dean'); _loc=_loc.replace(数据“-”+数据) 窗口位置=_loc; }else如果(_loc.search(order_regex_desc)!=-1){//有条件使其升序 控制台日志(无敌舰队); _loc=_loc.replace('-'+数据,数据) 窗口位置=_loc; //位置=\u位置替换(订单\u regex\u desc') }否则{ window.location=_loc+'&order='+数据; } } document.getElementById(“demo”).innerHTML=\u loc; } }); });
简单。

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <p id ='demo'></p>
    <button column-data='age'>Age</button>
    <button column-data='height'>Height</button>
    <button column-data='weight'>Weight</button>

  <script>
    document.getElementById("demo").innerHTML = 
    window.location.href;
    $(function() {
      $('button').click(function(){
        data = $(this).attr('column-data');
        order_regex_asc = new RegExp('[&?]order=' + data, 'g')
        order_regex_desc = new RegExp('[&?]order=-' + data, 'g')
        _loc = window.location.href

        //if(_loc.indexOf('?order=-'))
        zinData = '?order=' +  data;
        zinNegativeData = '?order=' + "-" +  data;

        if(_loc.indexOf(zinData) == -1){
          _loc = _loc.replace(zinNegativeData, '');
          data =  zinData;
        }
        else{
          _loc = _loc.replace(zinData, '');
          data =  zinNegativeData;
        }
        _loc = _loc +  data;
                window.location = _loc
          document.getElementById("demo").innerHTML = _loc;

      });
    });
  </script>
 </body>
</html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <p id ='demo'></p>
    <button column-data='age'>Age</button>
    <button column-data='height'>Height</button>
    <button column-data='weight'>Weight</button>

  <script>
    document.getElementById("demo").innerHTML = 
    window.location.href;
    $(function() {
      $('button').click(function(){
        data = $(this).attr('column-data');
        order_regex_asc = new RegExp('[&?]order=' + data, 'g')
        order_regex_desc = new RegExp('[&?]order=-' + data, 'g')
        _loc = window.location.href

        //if(_loc.indexOf('?order=-'))
        zinData = '?order=' +  data;
        zinNegativeData = '?order=' + "-" +  data;

        if(_loc.indexOf(zinData) == -1){
          _loc = _loc.replace(zinNegativeData, '');
          data =  zinData;
        }
        else{
          _loc = _loc.replace(zinData, '');
          data =  zinNegativeData;
        }
        _loc = _loc +  data;
                window.location = _loc
          document.getElementById("demo").innerHTML = _loc;

      });
    });
  </script>
 </body>
</html>
年龄 高度 重量 document.getElementById(“demo”).innerHTML= window.location.href; $(函数(){ $(“按钮”)。单击(函数(){ data=$(this.attr('column-data'); order_regex_asc=new RegExp('[&?]order='+数据,'g') order_regex_desc=新的RegExp('[&?]order=-'+数据,'g') _loc=window.location.href //如果(_loc.indexOf('?order=-')) zinData='?订单='+数据; ZinegativeData='?顺序='+“-”+数据; 如果(_loc.indexOf(zinData)=-1){ _loc=_loc.replace(ZinegativeData,”); 数据=数据; } 否则{ _loc=_loc.replace(zinData,”); 数据=数据; } _loc=_loc+数据; window.location=\u loc document.getElementById(“demo”).innerHTML=\u loc; }); });
简单。

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <p id ='demo'></p>
    <button column-data='age'>Age</button>
    <button column-data='height'>Height</button>
    <button column-data='weight'>Weight</button>

  <script>
    document.getElementById("demo").innerHTML = 
    window.location.href;
    $(function() {
      $('button').click(function(){
        data = $(this).attr('column-data');
        order_regex_asc = new RegExp('[&?]order=' + data, 'g')
        order_regex_desc = new RegExp('[&?]order=-' + data, 'g')
        _loc = window.location.href

        //if(_loc.indexOf('?order=-'))
        zinData = '?order=' +  data;
        zinNegativeData = '?order=' + "-" +  data;

        if(_loc.indexOf(zinData) == -1){
          _loc = _loc.replace(zinNegativeData, '');
          data =  zinData;
        }
        else{
          _loc = _loc.replace(zinData, '');
          data =  zinNegativeData;
        }
        _loc = _loc +  data;
                window.location = _loc
          document.getElementById("demo").innerHTML = _loc;

      });
    });
  </script>
 </body>
</html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <p id ='demo'></p>
    <button column-data='age'>Age</button>
    <button column-data='height'>Height</button>
    <button column-data='weight'>Weight</button>

  <script>
    document.getElementById("demo").innerHTML = 
    window.location.href;
    $(function() {
      $('button').click(function(){
        data = $(this).attr('column-data');
        order_regex_asc = new RegExp('[&?]order=' + data, 'g')
        order_regex_desc = new RegExp('[&?]order=-' + data, 'g')
        _loc = window.location.href

        //if(_loc.indexOf('?order=-'))
        zinData = '?order=' +  data;
        zinNegativeData = '?order=' + "-" +  data;

        if(_loc.indexOf(zinData) == -1){
          _loc = _loc.replace(zinNegativeData, '');
          data =  zinData;
        }
        else{
          _loc = _loc.replace(zinData, '');
          data =  zinNegativeData;
        }
        _loc = _loc +  data;
                window.location = _loc
          document.getElementById("demo").innerHTML = _loc;

      });
    });
  </script>
 </body>
</html>
年龄 高度 重量 document.getElementById(“demo”).innerHTML= window.location.href; $(函数(){ $(“按钮”)。单击(函数()