简体   繁体   English

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

[英]How can I hide a specific span class on a page with a dynamic ID?

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. 我已经尝试过使用CSS伪类,但无法使其正常工作。 I'm trying this with javascript but it's not working - as it hides the "details" class on both pages. 我正在尝试使用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();
  }
});

What you have to do is see if there is a parameter being passed to the url and then hide based on that. 您要做的就是查看是否有参数传递给url,然后根据该参数隐藏。 So here is a javascript function to get the parameter and the check for it: 因此,这是一个获取参数和检查参数的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>

All of the pages will include demosite.com/events - you're looking for pages that don't also have event_id s. 所有页面都将包含demosite.com/events您正在寻找的页面也没有event_id

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何隐藏包含特定值的类? - How can i hide class contain specific value? 如何在点击时显示/隐藏具有特定 class 的元素? - How can I show/hide elements with a specific class on click? 为什么我无法获取特定跨度的 ID 属性? - Why can't I get ID attribute of a specific span? 如何在不使用id的情况下创建动态css类? - How i can make a dynamic css class without use of id? 如何找到没有 id 和 class 定义的跨度元素 using.not() jQuery 内置 function - how can I find span element with no id and class defined by using .not() jQuery build-in function 如何使用 JQuery 在单击对象内检索具有特定类的 span 元素内的文本? - How can I retrieve the text inside a span element having a specific class that is inside a clicked object using JQuery? 如何单击位于具有 span 类的特定 div 中的第一个按钮? (使用javascript) - How can I click on the the first button, which is located in specific div with span class?? (using javascript) 如果 ID 变量为 0,则隐藏跨度 - Hide span if ID variable is 0 如何隐藏具有给定类的所有元素的父元素,但父元素也具有特定的类? - How can I hide parent element of all elements with a given class, but the parent element also has a specific class? 如何使用确定的类别隐藏或显示最近的跨度 - How to hide or show closest span with definite class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM