gpt4 book ai didi

javascript - 在更改该页面上的类时定向到另一个页面

转载 作者:太空宇宙 更新时间:2023-11-04 03:06:01 25 4
gpt4 key购买 nike

我有两个 html 页面,我需要将第一页链接到第二页,然后修改第二页上的元素类。

例如.. 第一页有两个按钮,如果您按下第一个按钮,您将被定向到第二页并且第二页背景变为绿色。如果您按下第二个按钮,您将获得相同的指示,但背景变为蓝色。

有没有办法做到这一点?不使用 jquery。

最佳答案

您可以通过查询字符串传递参数并使用第 2 页上的 document.location.search 获取参数并相应地设置颜色。

<a href="page2.html?color=green">Green</a>
<a href="page2.html?color=blue">Blue</a>

然后在第2页:

<script>
// Assuming the query string is ?color=blue,
// splitting on = will result in ["?color", "blue"].
// In the real world you shouldn't assume it's in
// exactly that format, but for the sake of example...

var color = window.location.search.split('=')[1];

// add the css class 'blue' to the body tag.
var document.body.classList.add(color);
</script>

在 CSS 中:

.blue {
background-color: blue;
}

.green {
background-color: green;
}

关于javascript - 在更改该页面上的类时定向到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30446238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com