基于 URL 的 ID 更改类别 - 基于 URL 的图像交换 -
我想要实现的目标: 根据 URL(即 foo.com/item1),div 元素“logoswap”接收不同的类。
以下是我整理的代码,但它似乎完全错误。无论如何,我不是 JS 专业人士,XHTML/CSS 更适合我的速度(一些 PHP)...我不能使用 PHP,即使它可以在 PHP 中使用(我知道这是因为我有一个 PHP 版本)我已经完成了,但我无法正确调用 PHP,
我只是想根据目录/url 显示不同的徽标...它不一定是名为的背景元素。必然由 CSS 类包含,我只需要根据上述内容加载不同的图像url 变量...
$(function() {
var url = location.pathname;
if(url.indexOf('item1') > -1) {
document.getElementById("logoswap").className += " class1";
}
elseif(url.indexOf('item2') > -1) {
document.getElementById("logoswap").className += "class2";
}
elseif(url.indexOf('item3') > -1) {
document.getElementById("logoswap").className += "class3";
}
elseif(url.indexOf('item4') > -1) {
document.getElementById("logoswap").className += "class4";
}
elseif(url.indexOf('item5') > -1) {
document.getElementById("logoswap").className += "class5";
}
else {
document.getElementById("logoswap").className += "class1";
}
});
这就是我所拥有的...丑陋,我确信
这就是我在这里的原因,我确实需要一些帮助。
What I'm trying to achieve:
Based on URL (ie., foo.com/item1), the div element "logoswap" receives a different class.
The following is the code I put together but it seems completely wrong. I'm not a JS pro by any means, XHTML/CSS is more my speed (some PHP)... I cannot use PHP, even if it is possible in PHP (and I know it is because I have a PHP version of what I need done already, but I can't call the PHP properly.
I'm really just trying to get a different logo to show up based on the directory/url... It doesn't have to be a background element called in by the CSS class necessarily, I just need a different image to load based on the aforementioned url variable...
$(function() {
var url = location.pathname;
if(url.indexOf('item1') > -1) {
document.getElementById("logoswap").className += " class1";
}
elseif(url.indexOf('item2') > -1) {
document.getElementById("logoswap").className += "class2";
}
elseif(url.indexOf('item3') > -1) {
document.getElementById("logoswap").className += "class3";
}
elseif(url.indexOf('item4') > -1) {
document.getElementById("logoswap").className += "class4";
}
elseif(url.indexOf('item5') > -1) {
document.getElementById("logoswap").className += "class5";
}
else {
document.getElementById("logoswap").className += "class1";
}
});
That's what I have... Ugly I'm sure.
That's why I'm here though, I definitely need some help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过 URL 路径名分配 CSS 类
这是使用数字表达式(如果可用)的情况。这不适用于上述问题。
更新:
根据新的详细信息,您可能需要一组值,这些值应该派生自或完全等于您打算使用的类名。
为了使这项工作顺利进行,您需要做好一些准备工作。我假设路径将以数字结尾,正如我们在此处概述的那样。默认以
1
结尾。您需要可以访问这些图像。您需要为每种可能性定义样式。CSS 设置
没有 jQuery 的
如果您的代码中没有 jQuery,您可能需要使用
window.onload
。Assigning CSS Class By URL Pathname
Here is a case for using numeric expressions if they are available. This does not apply to the above question.
UPDATE:
In light of the new details you may need an array of values, these should be derived from or exactly equal to the class names you intend to use.
To make this work you will need a few things in place. I will assume that the paths will end in a number as we have outlined here. The default ends with
1
. You will need the images to be accessible. You need to define the styles for each possibility.CSS Setup
Without jQuery
if you do not have jQuery in your code you may need to use
window.onload
.你所拥有的并没有什么问题。你可以用下面的东西来整理它。
希望这有帮助。
There's nothing that wrong with what you have. You could tidy it up with something like below.
Hope this helps.
仅使用 HTML/CSS,您可以将
id
添加(或通过 javascript 附加)到页面的body
:然后在 CSS 中创建一个选择器:
Using just HTML/CSS, you could add (or append via javascript) an
id
to thebody
of the page:Then in your CSS, create a selector: