根据URL部分更改背景图像

时间:2013-07-10 22:50:44

标签: javascript jquery css

我正在尝试向我的网站添加功能,其中背景将根据使用样式表的部分URL进行更改。

示例:

address/shop/index.php             = background image 1
address/shop/index.php?cPath=22    = background image 2
address/shop/index.php?cPath=23    = background image 3
address/shop/index.php?cPath=24    = background image 4

有什么想法吗?我看过Javascript和jQuery,但不确定选择哪一个或如何去做。

4 个答案:

答案 0 :(得分:3)

看起来你正在使用PHP,所以你应该能够在没有javascript的情况下完成它。您只需要使用PHP的服务器变量。

这样的东西
 $cPath = $_GET['cPath']; //This allows access to the query part of the url
 if($cPath == 24){
     //set background url
 }

答案 1 :(得分:0)

您可以随时让网址的部分被标记包围,例如<font> 所以代码就像:

<a>your url link 
    <font class='background1'>part with background1</font>
    <font class='background2'>part with background2</font>
</a>

然后在css中你可以有

.background1{background: url('...')}
.background2{background: url('...')}

这可能是一种hackish方式,但通过这种方式,您至少可以对“链接部分”进行分类。

答案 2 :(得分:0)

如果你真的想做这个客户端,我建议:

var pageBackground = {
    'default' : 'classOne',
    '22' : 'classTwo',
    '23' : 'classThree'
};

document.body.className = pageBackground[window.location.split('cPath=')[1] || 'default'];

与样式表相结合:

body.classOne {
    background-image: url(path/to/defaultImage.png);
}
body.classTwo {
    background-image: url(path/to/imageTwo.png);
}
body.classThree {
    background-image: url(path/to/imageThree.png);
}

答案 3 :(得分:-1)

在body标签类型的html页面上:

<body background="your file">

</body>

您可以在每个想要不同背景的页面上执行此操作,并且它将覆盖任何为主体设置样式的CSS样式