想知道是否有可能隐藏或显示基于多个URL的内容?根据多个网址隐藏/显示内容
例如,用户在家或索引与www.url.com/或www.url.com/index.html
特定元素display: block
或display: none
if(location=="http://domain.com/") {
伟大的作品,但我怎么能指定多个URL上面类似的格式相同吗?
想知道是否有可能隐藏或显示基于多个URL的内容?根据多个网址隐藏/显示内容
例如,用户在家或索引与www.url.com/或www.url.com/index.html
特定元素display: block
或display: none
if(location=="http://domain.com/") {
伟大的作品,但我怎么能指定多个URL上面类似的格式相同吗?
if(location=="http://google.com") {
//Do something here
} else {
//Do something else here
}
通过测试location.pathname
这样的:
document.getElementById('specific').style.display = location.pathname === "/index.html" ? 'none' : 'block';
这不提供问题的答案。要批评或要求作者澄清,请在其帖子下方留言。 – 2014-10-02 21:09:37
好吧然后我改变它 – OxyDesign 2014-10-02 21:58:05
您可以使用sammy.js(http://sammyjs.org/)。它有一个很好的URL路由功能。
您可以使用PHP到$_GET
URL信息。比方说,你有以下网址:
https://www.wherever.com/some_file.php?css1=1&css2=1
在另一个时间,让我们假设URL看起来像:
https://www.wherever.com/some_file.php?css3=1&css4=1
当你写一个生成HTML的PHP页面:
<?php
$css = '';
function link_builder(){
$a = func_get_args(); $lo = "<link rel='stylesheet' type='text/css' href='"; $lc = "' />"; $o = '';
foreach($a as $v){
$o .= "$lo$v$lc";
}
return $o;
}
if(isset($_GET['css1'], $_GET['css2']) && $_GET['css1'] === '1' && $_GET['css2'] === '1'){
$css = link_builder('cssPath1.css', 'cssPath2.css');
}
if(isset($_GET['css3'], $_GET['css4']) && $_GET['css3'] === '1' && $_GET['css4'] === '1'){
$css = link_builder('cssPath3.css', 'cssPath4.css');
}
echo $css;
?>
使用此方法,您可以预先创建单独的外部CSS文件,这些文件可以缓存到客户端的浏览器内存中。 JavaScript风格不会那样做。
谢谢!不知道为什么我会陷入低谷,只是从来没有尝试过这种技术。感谢寿 – 2014-10-02 20:59:31