使用 href 在 HTML 中传递参数
Using href to pass argument in HTML
我可以使用 html 中的以下行来传递带有 link
的静态值
<button class="light" id = "pg1" onclick="window.location.href = '1.html?theme=light';">page 1</button>
我在此处附加了 ?theme=light
以便稍后可以在 1.html
中检索 theme
的值
我的问题: 有没有办法让我发送 class 变量本身?
即, 如果 class="dark"
有没有办法可以在不自己更改参数的情况下将主题作为 ?theme=dark
发送?
您必须使用 JS 函数读取当前对象的 class(您已经在 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>
我可以使用 html 中的以下行来传递带有 link
的静态值<button class="light" id = "pg1" onclick="window.location.href = '1.html?theme=light';">page 1</button>
我在此处附加了 ?theme=light
以便稍后可以在 1.html
theme
的值
我的问题: 有没有办法让我发送 class 变量本身?
即, 如果 class="dark"
有没有办法可以在不自己更改参数的情况下将主题作为 ?theme=dark
发送?
您必须使用 JS 函数读取当前对象的 class(您已经在 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>