douchui7332 2018-03-05 01:57
浏览 124
已采纳

如何使用多个url参数来显示/隐藏多个div

Hoping to find a way to have multiple URL parameters display specific divs for certain products, while hiding all the other product divs.

For example:

website.com/?product=widget1,widget3,widget4

or

website.com/?product=widget1&product=widget3&product=widget4

The above would display the 3 divs associated with each of those 3 parameters and hide all others. See below.

<div class="widget1 product-selector">Widget1</div> SHOW

<div class="widget2 product-selector">Widget2</div> HIDE

<div class="widget3 product-selector">Widget3</div> SHOW

<div class="widget4 product-selector">Widget4</div> SHOW

<div class="widget5 product-selector">Widget5</div> HIDE

The example on the following site achieves similar, but it will only allow for a single URL parameter: http://jennamolby.com/how-to-display-dynamic-content-on-a-page-using-url-parameters/

Appreciate any solutions to help me achieve this. Thanks

  • 写回答

1条回答 默认 最新

  • duandi5328 2018-03-05 02:49
    关注

    Query parameter should have unique variable name. Keep only one parameter 'product' and values should be separated by some special character like pipe/comma. Try following solutions, it use one parameter.

    http://localhost:8080/test.html?product=widget2,widget3

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Document</title>
      <style>
      .product-selector{
      display: none;
    }
      </style>
    </head>
    <body>
      <div class="widget1 product-selector">Widget1</div>
    
    <div class="widget2 product-selector">Widget2</div>
    
    <div class="widget3 product-selector">Widget3</div>
    
    <div class="widget4 product-selector">Widget4</div>
    
    <div class="widget5 product-selector">Widget5</div>
    <script>
    (function() {
      function getParameterByName(name, url) {
        if (!url) url = window.location.href;
        name = name.replace(/[\[\]]/g, "\\$&");
        var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
            results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, " "));
      }
      var products = getParameterByName('product');
      products = products.split(',');
      products.forEach((product) => {
        var el = document.getElementsByClassName(product)[0];
        el.style.display = 'block';
      });
    })();
    </script>
    </body>
    </html>

    Let me know if this solve your problem.

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题