Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在具有动态ID的页面上隐藏特定的span类?_Javascript_Jquery - Fatal编程技术网

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

Javascript 如何在具有动态ID的页面上隐藏特定的span类?,javascript,jquery,Javascript,Jquery,我试图在所有单个事件页面上隐藏一个名为“details”的类,但不在我的主事件页面上 主事件页面是demosite.com/events/ 单个事件页面为demosite.com/events/?event\u id=2 我尝试过css伪类,但无法使其工作。我正在用javascript尝试这一点,但它不起作用——因为它在两个页面上都隐藏了“details”类 这就是我到目前为止所尝试的 $(function(){ var url = document.location.href; if

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

主事件页面是
demosite.com/events/
单个事件页面为
demosite.com/events/?event\u id=2

我尝试过css伪类,但无法使其工作。我正在用javascript尝试这一点,但它不起作用——因为它在两个页面上都隐藏了“details”类

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

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

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

您需要做的是查看是否有一个参数被传递到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>

$(文档).ready(函数(){
var eventid=getParameterByName('event_id');
如果(eventid!=“”){
$('.details').hide();
}
否则{
$('.details').show();
}
});
函数getParameterByName(名称){
name=name.replace(/[\[]/,“\\[”)。replace(/[\]]/,“\\]”);
var regex=new RegExp(“[\\?&]”+name+“=([^&\]*)”,
results=regex.exec(location.search);
返回结果===null?”:decodeURIComponent(结果[1]。替换(/\+/g,”);
}
所有页面都将包含
demosite.com/events
-您正在查找的页面也没有
事件id
s

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

如果您使用的是jQuery,请将标签添加到您的问题中。您不应该检查URL是否包含
event\u id=
?在我看来,它总是包含
http://demosite.com/events/
但是查询字符串参数是可选的……您当前的版本与这两个页面匹配。将
?event_id=
添加到
indexOf()
的字符串参数中,如
.indexOf('http://demosite.com/events/?event_id=)
。谢谢ochi和juan.facorro!这正是我需要的。我的逻辑是颠倒的。根据你的建议修正的论点很有魅力!!!