根據 URL 變量過濾 HTML 內容 (Filtering HTML content based on URL Variable)


問題描述

根據 URL 變量過濾 HTML 內容 (Filtering HTML content based on URL Variable)

I am looking for a way that I can hide specific HTML content based on a variable in the URL.

For example, I am passing a variable: index.html?app=new

I have various images and other content that has a prod attribute and would like to show only those that have the "new" prod value.

<img prod="new" class="image" src="../images/screen.png" height="300" width="600"/>

I have used the following to harvest the url variable:

<script>  
  function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
    function(m,key,value) {
    vars[key] = value;
    });
    return vars;
  }
</script>

My problem is where to go from here


參考解法

方法 1:

I think this CSS rule will do the magic for you:

[prod]:not([prod=new]) { display:none; }

It will hide all elements having 'prod' attribute with the value other than 'new'.

方法 2:

This is untested code.

HTML

<img data‑prod="new" class="image" src="../images/screen.png" height="300" width="600" />

JavaScript

<script>  
    (function(){
    'use strict';

    var getUrlVars = function() {
        var vars = {};

        window.location.href.replace(
            /[?&]+([^=&]+)=([^&]*)/gi,
            function(m,key,value) {
                vars[key] = value;
            }
        );
        return vars;
    };

    if(typeof getUrlVars.app === 'string' && getUrlVars.app === 'new') {
        var c = document.querySelectorAll('[data‑prod="new"]'), i;
        for(i = 0; i < c.length; i+=1) {
            c[i].style.display = 'none';
        }
    }
    })();
</script>

In the general case (i.e. app=new, app=whatever), you can substitute new in the querySelectorAll parameter as the variable itself:

if(typeof getUrlVars.app === 'string') {
    var c = document.querySelectorAll('[data‑prod="' + getUrlVars.app + '"]'), i;
    for(i = 0; i < c.length; i+=1) {
        c[i].style.display = 'none';
    }
}

(by kmanc‑smilerink.attendant.6)

參考文件

  1. Filtering HTML content based on URL Variable (CC BY‑SA 3.0/4.0)

#filtering #css #url #html






相關問題

禁止查找和 grep“無法打開”輸出 (Suppress find & grep "cannot open" output)

directx10 中的精靈和紋理過濾 (Sprites in directx10 and texture filtering)

使用 Lucene 統計分類結果 (Using Lucene to count results in categories)

根據 URL 變量過濾 HTML 內容 (Filtering HTML content based on URL Variable)

網格列包含 int64 值,但過濾器顯示字符串並且不起作用/ (Grid column contains int64 values but filter shows strings and doesn't work/)

angularjs中的雙重過濾 (dual filtering in angularjs)

按序列順序過濾 Birt 表 (Filter in serial order for Birt tables)

日期的 Drupal 8 上下文過濾器 (Drupal 8 contextual filter for date)

在VB中過濾一個wpf collectionviewsource? (Filter a wpf collectionviewsource in VB?)

遍歷對象並返回匹配的鍵/值 (Traverse object and return matching key / values)

過濾Python列表時出現意外輸出:我做錯了什麼? (Unexpected output when filtering Python list: What am I doing wrong?)

角度:錯誤 RangeError:超出最大調用堆棧大小 (ANGULAR: ERROR RangeError: Maximum call stack size exceeded)







留言討論