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 angular开发过程中,想要读取模型文件,即图1的335行,会报404错误(如图2)。但我的springboot里配置了静态资源文件,如图3。且在该地址下我有模型文件如图4,请问该问题该如何解决呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置
  • ¥15 Java计划序号重编制功能,此功能会对所有序号重新排序,排序后不改变前后置关系。
  • ¥15 关于哈夫曼树应用得到一些问题