更改页面时可以更改脚本中的类名吗

发布于 2024-11-26 02:35:31 字数 754 浏览 0 评论 0原文

大家好,
我有一个问题。我编写了一个非常广泛的脚本来将我的身体背景从特定于页面的背景图像更改为其他背景图像。供参考:

我的“mypage.html”在 default 样式表中定义了默认背景 class='image1'。
我编写了一个脚本来将其更改为class='image10' 或 class='image11',它们在持久样式表中定义。 (相信我,这是简短的版本,但这部分有效......好吧,将会。这里没有问题。)
我的“otherpage.html”有一个默认背景 class='image2' 定义在默认样式表,我希望能够将其更改为持久样式表中相同的“image10”和“image11”。
两个默认背景都有多个不同颜色的版本备用样式表...将样式表和 class='image1' 链接更改为图像的另一个版本。
所有这些都由特定于页面的 cookie 控制。这使得找到解决方案变得非常重要,因为否则我将不得不为每个页面设置 cookie。我觉得这是不可接受的。
我的问题是,我是否必须将整个脚本复制/粘贴到我的“otherpage.html”并将所有“image1”更改为“Image2”,或者有什么办法javascript 类似:

if(HTML = 'otherpage.html') {
   (".image1" = ".image2")
}

请原谅我非常业余的脚本。我已经全部看过,但我什至不知道如何搜索这个...希望有人可以提供帮助,否则复制/过去它是;)

Hallo All,
I have a problem. I have written a very extensive script to change my body-background from a page-specific background-image to an other background-image. For refrence:

My 'mypage.html' has a default background of class='image1' defined in the default stylesheet.
I have written a script to change this to class='image10' or class='image11', which are defined in the persistent stylesheet. (Believe me, this is the short version, but this part works... well, is going to. No questions here.)
My 'otherpage.html' has a default background of class='image2' defined in the default stylesheet and I want to be able to change this as well to the same 'image10' and 'image11' from the persistent stylesheet.
Both default backgrounds have multiple differently colored versions in alternate stylesheets... Change the stylesheet and class='image1' links to another version of the image.
All this is directed by cookies that are page specific as well. This makes finding a solution quite important, because otherwise I would have to set cookies for every single page. Which I find unacceptable.
My question is, do I have to copy/past the whole script to my 'otherpage.html' and change al the 'image1's to 'Image2's or is there a way to javascript something like:

if(HTML = 'otherpage.html') {
   (".image1" = ".image2")
}

Excuse my very amateuristic script. I have looked all over, but I wouldn't even know how to search for this... Hope someone can help, otherwise copy/past it is ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮色兮凉城 2024-12-03 02:35:31

您可以在此处使用这个小脚本

var currentPage =  window.location.pathname;

if (currentPage == "/somepage.html") {
   // change the body class name
   document.getElementsByTagName("body")[0].className = "image2";
}

。如果您有多个页面要测试,则可以使用 switch 语句。

You can use this little script here

var currentPage =  window.location.pathname;

if (currentPage == "/somepage.html") {
   // change the body class name
   document.getElementsByTagName("body")[0].className = "image2";
}

You can use a switch statement if you have multiple pages to test.

放飞的风筝 2024-12-03 02:35:31

您可以通过基类控制样式(背景和其他样式),您可以将其设置为文档或任何根容器的 body 标签。在页面加载时检查页面名称并相应地设置类名称。

if(page == "otherPage"){
  document.body.className = "image2";
}

现在所有属于 image2 的类都将被应用。

You can control the styles(background and other styles) through the base class which you can set it to body tag of the document or any root container. On page load check the page name and set the class names accordingly.

if(page == "otherPage"){
  document.body.className = "image2";
}

Now all the classes falling under image2 will be applied.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文