【问题标题】:display DIVs for specific URLs显示特定 URL 的 DIV
【发布时间】:2017-02-03 09:50:00
【问题描述】:

有没有办法根据 URL 显示不同的 DIV?

我有一个包含 3 个不同子事件的事件网站,因此我想在导航中显示每个单独事件(多个 URL)的事件日期。

    <div id="us" class="us-web-date">&nbsp;• San Jose | May 1•2•3</div>
    <div id="emea" class="emea-web-date">&nbsp;• Berlin | May 15</div>
    <div id="de" class="de-web-date">&nbsp;• Berlin | 16. Mai</div>

所以https://acrolinxcc.staging.wpengine.com/cc2017-us-about/(以及所有我们的页面)只会显示 div id="us"。因此,对于 /cc2017-emea-about/(和所有 emea 页面)只会显示 id="emea" 和 /cc2017-de-ueber-die-konferenz/(和所有 de-pages)只会显示 id="de ”。

【问题讨论】:

标签: javascript php css


【解决方案1】:

你可以通过 JavaScript 来处理它。

步骤:

  • 获取当前网址
  • 拆分网址
  • 查找单词是否存在
  • 基于此隐藏/显示 div

示例:

var currentUrl = window.location.pathname;
var segments = currentUrl,split('/');   // Split the url using '/'

// Fetch the part that you want to use to search specific word
// in your case it will be at index 1

if (0 < segments[1].search('us')) {
  // show div us
} else if (0 < segments[1].search('emea')) {
  // show div emea
} else {
  // show div de
}

您应该在加载时编写此代码。 我希望它会有所帮助。

【讨论】:

    【解决方案2】:

    你可以用php做到这一点

    您可以通过$_SERVER['REQUEST_URI']获取当前URI,并通过strpos()检查您的身份

    你的代码会是这样的

    <?php
       $url =  $_SERVER['REQUEST_URI'];
       if(strpos($url, "-us-")){ ?>
           <div id="us" class="us-web-date">&nbsp;• San Jose | May 1•2•3</div>
       <?php } elseif (strpos($url, "-emea-")) { ?>
           <div id="emea" class="emea-web-date">&nbsp;• Berlin | May 15</div>
       <?php } elseif (strpos($url, "-de-")) { ?>
           <div id="de" class="de-web-date">&nbsp;• Berlin | 16. Mai</div>
       <?php } ?>
    

    【讨论】:

      猜你喜欢
      • 2011-08-11
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      相关资源
      最近更新 更多