在HTML中使用href来传递参数

问题描述 投票:0回答:1

我可以在html中使用下面这一行来传递一个带有链接的静态值。

<button class="light" id = "pg1" onclick="window.location.href = '1.html?theme=light';">page 1</button>

我在此附上 ?theme=light 的值,这样我以后就可以检索到 theme1.html

我的问题是:有没有办法让我自己发送类变量? 有没有办法让我发送类变量本身?

class="dark" 有什么办法可以让我把主题作为 ?theme=dark 而不自己修改参数?

html href
1个回答
2
投票

你必须使用JS函数读取当前对象的类(你已经在onClick事件中使用了JavaScript)。

<script type="text/javascript">
  function setLink(obj){
    window.location.href = '1.html?theme=' + obj.className;
  }
</script>

<button class="light" id = "pg1" onclick="setLink(this);">page 1</button>
© www.soinside.com 2019 - 2024. All rights reserved.