我有两个html页面,我需要将第一个页面链接到第二个页面,然后修改第二个页面上的一个元素类。
例如..第一页有两个按钮,如果你按第一个按钮,你会被引导到第二页,第二页的背景变成绿色。如果你按下第二个按钮,你会得到相同的方向,但背景变成蓝色。
有没有办法做到这一点?不使用jquery。
发布于 2015-05-26 05:41:18
您可以通过查询字符串传递一个参数,并使用第2页上的document.location.search获取参数并相应地设置颜色。
<a href="page2.html?color=green">Green</a>
<a href="page2.html?color=blue">Blue</a>然后在page2上:
<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;
}https://stackoverflow.com/questions/30446238
复制相似问题