如何在具有动态ID的页面上隐藏特定的span类?

时间:2022-11-25 18:10:22

I'm trying to hide a class called "details" on all single event pages but not on my main events page.

我试图在所有单个事件页面上隐藏一个名为“details”的类,但不是在我的主事件页面上。

The main event page is demosite.com/events/ The single event pages are demosite.com/events/?event_id=2.

主要活动页面是demosite.com/events/单个活动页面是demosite.com/events/?event_id=2。

I've tried with css pseudo classes but can't get it to work. I'm trying this with javascript but it's not working - as it hides the "details" class on both pages.

我已尝试使用css伪类但无法使其工作。我正在尝试使用javascript,但它不起作用 - 因为它隐藏了两个页面上的“详细信息”类。

This is what I've tried so far.

这是我到目前为止所尝试的。

$(function(){
  var url = document.location.href;

  if (url.toLowerCase().indexOf('http://demosite.com/events/') >= 0) {
    $('.details').hide();
  } else {
    $('.details').show();
  }
});

2 个解决方案

#1


1  

All of the pages will include demosite.com/events - you're looking for pages that don't also have event_ids.

所有页面都将包含demosite.com/events - 您正在寻找没有event_ids的页面。

if (document.location.search.indexOf('event_id=') >= 0))
  $('.details').hide();
} else {
  $('.details').show();
}

#2


1  

What you have to do is see if there is a parameter being passed to the url and then hide based on that. So here is a javascript function to get the parameter and the check for it:

你要做的是看看是否有一个参数传递给url然后根据它隐藏。所以这里有一个javascript函数来获取参数并检查它:

<script>
 $(document).ready(function() {
    var eventid = getParamterByName('event_id');
    if ( eventid != "" ) {
       $('.details').hide();
    }
    else {
      $('.details').show();
    }

});

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, " "));
}


</script>

#1


1  

All of the pages will include demosite.com/events - you're looking for pages that don't also have event_ids.

所有页面都将包含demosite.com/events - 您正在寻找没有event_ids的页面。

if (document.location.search.indexOf('event_id=') >= 0))
  $('.details').hide();
} else {
  $('.details').show();
}

#2


1  

What you have to do is see if there is a parameter being passed to the url and then hide based on that. So here is a javascript function to get the parameter and the check for it:

你要做的是看看是否有一个参数传递给url然后根据它隐藏。所以这里有一个javascript函数来获取参数并检查它:

<script>
 $(document).ready(function() {
    var eventid = getParamterByName('event_id');
    if ( eventid != "" ) {
       $('.details').hide();
    }
    else {
      $('.details').show();
    }

});

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, " "));
}


</script>