[英]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.