将变量放置为 HREF
基本上,是否可以做类似的事情......
<a href=link onClick="clicked();">Click me!</a>
<script>
function clicked() {
if(myVar == 1) {
link="http://stackoverflow.com"
}
else if (myVar == 2) {
link="http://google.com"
}
}
</script>
这个例子可能是不可能的,因为它同时触发...... 但是否可以在那里使用变量呢?
基本上,我需要一个链接,根据变量将您带到两个不同的地方。 我想我可以有两个链接,然后根据变量分别隐藏/显示每个链接,但我想知道是否有可能另一种方式?
我正在使用 HTML、CSS、JavaScript 和 JQuery...
谢谢!
Basically, is it possible to do something like....
<a href=link onClick="clicked();">Click me!</a>
<script>
function clicked() {
if(myVar == 1) {
link="http://stackoverflow.com"
}
else if (myVar == 2) {
link="http://google.com"
}
}
</script>
This example is probably impossible due to it firing at the same time...
But is it at all possible to use variables there?
Basically, I need a link that'll bring you to two different places depending on a variable.
I suppose I could have two links, and just hide/show each one respectively depending on the variable, but I was wondering if it's possible another way?
I'm working with HTML, CSS, JavaScript, and JQuery...
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你可以这样做:
you could do this:
试试这个
干杯。这将根据 myVar 值将您带到所需的位置!
Try this out
Cheers. This will take you to the desired place based on the myVar values!
但我看到大家都这么说。只需检查哪种方法适用于大多数浏览器
But I see everyone said about the same. Just check which of the methods works on most browsers
你可以这样做...
如果满足条件,那么它会带你到一个不同的URL。
否则,该链接将按预期工作。
如果你不想使用 jQuery,那就......
You could do...
If the condition is met, then it will take you to a different URL.
Otherwise, the link will work as expected.
If you didn't want to use jQuery, that'd be...
您可以简单地使用 Javascript 进行重定向:
You could simply use Javascript to do a redirect:
不,不可能按照你想要的方式去做。
你为什么不这样做呢 -
请访问 Mozilla 开发人员中心,获取有关该窗口的更多参考信息。位置对象。
No it's not possible to do it the way you want.
Why don't you do this instead -
Take a look at Mozilla Developer Center for further references about the
window.location
object.