【发布时间】:2021-10-29 13:07:18
【问题描述】:
我找到了类似的答案,但不是我想要的。 如何处理具有 1 个功能的 3 个按钮?如何将文档属性集成到解决方案中?
代码如下:
function gotoUrl() {
window.location.assign("https://www.google.com/")
}
function gotoUrl1() {
window.location.assign("https://www.yahoo.com/")
}
function gotoUrl2() {
window.location.assign("https://www.youtube.com/")
}
<button class="one" onclick="gotoUrl()"> Google </button>
<button class="two" onclick="gotoUrl1()"> yahoo </button>
<button class="three" onclick="gotoUrl2()"> Youtube </button>
【问题讨论】:
-
为什么不直接使用链接呢?
-
onclick="gotoUrl('https://www.google.com')"然后function gotoUrl(url) { window.location.assign(url) }。但是链接确实会更简单...... -
@JeremyThille,对于真正知道链接重定向到哪里的用户来说更简单更好。
-
这是我做的家庭作业的一部分,现在已经 3 天了 :-)
-
作业是否也建议使用
onclick?无论您上什么课程,都会教授非常糟糕的做法。
标签: javascript button window.location