将变量放置为 HREF

发布于 2024-11-04 17:07:45 字数 491 浏览 0 评论 0原文

基本上,是否可以做类似的事情......

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蓝眸 2024-11-11 17:07:46

你可以这样做:

<script> function clicked() {
    if(myVar == 1) {
        window.location="http://stackoverflow.com"
    }
    else if (myVar == 2) {
        window.location="http://google.com"
    } } 
</script>

you could do this:

<script> function clicked() {
    if(myVar == 1) {
        window.location="http://stackoverflow.com"
    }
    else if (myVar == 2) {
        window.location="http://google.com"
    } } 
</script>
许仙没带伞 2024-11-11 17:07:46

试试这个

<a href=link onClick="clicked();">Click me!</a>

<script>
function clicked() {
    if(myVar == 1) {
        location.replace("http://stackoverflow.com");
    }
    else if (myVar == 2) {
        location.replace("http://google.com");
    }
}
</script>

干杯。这将根据 myVar 值将您带到所需的位置!

Try this out

<a href=link onClick="clicked();">Click me!</a>

<script>
function clicked() {
    if(myVar == 1) {
        location.replace("http://stackoverflow.com");
    }
    else if (myVar == 2) {
        location.replace("http://google.com");
    }
}
</script>

Cheers. This will take you to the desired place based on the myVar values!

月寒剑心 2024-11-11 17:07:46
function clicked() {
    var dest = "";
    if(myVar) 
       dest = "http://stackoverflow.com";
    else
       dest = "http://google.com"
    window.navigate(dest);

但我看到大家都这么说。只需检查哪种方法适用于大多数浏览器

function clicked() {
    var dest = "";
    if(myVar) 
       dest = "http://stackoverflow.com";
    else
       dest = "http://google.com"
    window.navigate(dest);

But I see everyone said about the same. Just check which of the methods works on most browsers

似梦非梦 2024-11-11 17:07:45

你可以这样做...

$('a').click(function(event) {
    if (condition) {
        event.preventDefault();
        window.location = 'http://different-url.com';
    }
});

如果满足条件,那么它会带你到一个不同的URL。

否则,该链接将按预期工作。

如果你不想使用 jQuery,那就......

var anchors = document.getElementsByTagName('a');
for (var i = 0, anchorsLength; i < anchorsLength; i++) {
   anchors[i].onclick = function(event) {
      if (condition) {
          event.preventDefault();
          window.location = 'http://different-url.com';
      }
   }
}

You could do...

$('a').click(function(event) {
    if (condition) {
        event.preventDefault();
        window.location = 'http://different-url.com';
    }
});

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...

var anchors = document.getElementsByTagName('a');
for (var i = 0, anchorsLength; i < anchorsLength; i++) {
   anchors[i].onclick = function(event) {
      if (condition) {
          event.preventDefault();
          window.location = 'http://different-url.com';
      }
   }
}
随梦而飞# 2024-11-11 17:07:45

您可以简单地使用 Javascript 进行重定向:

<script>
function clicked() {
    if(myVar == 1) {
        window.location = "http://url1.com";
    }
    else if (myVar == 2) {
        window.location = "http://url2.com";
    }
}
</script>

You could simply use Javascript to do a redirect:

<script>
function clicked() {
    if(myVar == 1) {
        window.location = "http://url1.com";
    }
    else if (myVar == 2) {
        window.location = "http://url2.com";
    }
}
</script>
执手闯天涯 2024-11-11 17:07:45

不,不可能按照你想要的方式去做。

你为什么不这样做呢 -

<a href="http://www.example.com" onClick="clicked();">Click me!</a>

<script>
function clicked()
{
    if(myVar == 1) 
    {
        window.location.href = "www.stackoverflow.com";
    }
    else if (myVar == 2)
    {
        window.location.href = "www.google.com";
    }
}

请访问 Mozilla 开发人员中心,获取有关该窗口的更多参考信息。位置对象。

No it's not possible to do it the way you want.

Why don't you do this instead -

<a href="http://www.example.com" onClick="clicked();">Click me!</a>

<script>
function clicked()
{
    if(myVar == 1) 
    {
        window.location.href = "www.stackoverflow.com";
    }
    else if (myVar == 2)
    {
        window.location.href = "www.google.com";
    }
}

Take a look at Mozilla Developer Center for further references about the window.location object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文