簡體   English   中英

如何通過在網址末尾添加指定的參數來隱藏網頁上的任何div?

[英]How to hide any div on a webpage by adding a specified parameter at the end of url?

我是一個博客作者,目前正在學習html。 因此,我對javascript的了解不多。 現在,我想知道如何通過添加這樣的參數來隱藏網頁中的任何div。 意味着如果我在URL末尾添加?hide=header-wrapper ,它應該隱藏header-wrapper 我可以使用javascript嗎?

我想要正確的代碼來執行此操作。 而且我想知道如何使代碼自動檢測div並隱藏它,就像我在URL中?hide=divID

任何幫助,將不勝感激。 提前致謝。

這是一種方法:

function getUrlParameter(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) 
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) 
        {
            return sParameterName[1];
        }
    }
}

$(document).ready(function() {
    var toHide = getUrlParameter('hide');

    if (toHide) {
        $('#' + toHide).hide();
    }
})

您也可以使用它。

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

$(document).ready(function() {
    var hide_id= getParameterByName('hide');


       /*just pass the name of query string parameter that you want to hide like
 in your url (`?hide=header-wrapper`) id that you want to hide is `header-wrapper`and query
 string parametername is `hide` so use getParameterByName('hide') */

if (hide_id) {
        $('#'+hide_id).hide();
    }
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM